Django GenericForeignKey limit to ContentTypes that inherit from a particular abstract model
<p>In my application's models I need a way of linking <code>Problems</code> and <code>Solutions</code> -- every <code>Problem</code> can have multiple <code>Solutions</code> and a given <code>Solution</code> can map back to multiple <code>Problems</code>.  </p>

<p><code>Solution</code> is an abstract base class, since there can be many varieties of <code>Solutions</code>.  So, I figured out I need a mapping table <code>ProblemSolutionMapping</code> which uses a <code>GenericForeignKey</code> to accommodate all those child classes.  But I'm trying to figure out how to limit the classes to just the children of <code>Solutions</code> and not all the classes available in the whole application, which is what is currently happening.  </p>

<pre><code># Thanks to http://stackoverflow.com/a/23555691/1149759
class Solution(models.Model):
    ...
    @classmethod
    def get_subclasses(cls):
        content_types = ContentType.objects.filter(app_label=cls._meta.app_label)
        models = [ct.model_class() for ct in content_types]
        return [model for model in models
                if (model is not None and
                    issubclass(model, cls) and
                    model is not cls)]

    class Meta:
        abstract = True


class ProblemSolutionMapping(models.Model):
    problem = models.ForeignKey(Problem)
    content_type = models.ForeignKey(ContentType,
        limit_choices_to=Solution.get_subclasses()) # &lt;==== This is the issue
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')
</code></pre>

<p>The issue is that when I start up my Django app, the call to <code>ContentType.objects.filter(app_label=cls._meta.app_label)</code> throws the error:</p>

<pre><code>django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
</code></pre>

<p>Not sure what to do -- I tried making the mapping table the last one in the relevant models file (all the child classes are defined above it in the same file), but it made no difference.  <strong>Is this something that I have to move into the admin form?  Or is there some other way to do this at the model level?</strong>  </p>

<p>(Django 1.9, in case it matters.)</p>

<p>Thanks in advance for your help!</p>