Django 1.7'list_filter'引发AppRegistryNotReady(“模型尚未加载。”)

时间:2014-09-23 11:45:41

标签: django python-2.7

每当我试图包含list_filter时,Django(1.7)都会引发AppRegistryNotReady(“模型尚未加载。”)。

class ChoiceAdmin(admin.ModelAdmin):
    list_display = ('choice_text', 'votes')


class ChoiceInline(admin.StackedInline):
    model = choice.Choice
    extra = 4


@admin.register(Question)
class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        (None, {'fields': ['question_text']}
         )]
    search_fields = ['question_text']
    list_filter = ['pub_date']
    inlines = [ChoiceInline]   

下面我已经包含了错误追溯:

    Traceback (most recent call last):
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\theindianpoll_repo_container\theindianpoll_dev_repo\theindianpoll\manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\lib\site-packages\django\core\management\__init__.py", line 354, in execute
    django.setup()
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\lib\site-packages\django\__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\lib\site-packages\django\apps\registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\lib\site-packages\django\apps\config.py", line 197, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Python27\Lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\theindianpoll_repo_container\theindianpoll_dev_repo\theindianpoll\polls\models.py", line 19, in <module>
    class QuestionAdmin(admin.ModelAdmin):
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\lib\site-packages\django\contrib\admin\decorators.py", line 25, in _model_admin_wrapper
    admin_site.register(models, admin_class=admin_class)
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\lib\site-packages\django\contrib\admin\sites.py", line 99, in register
    admin_class.check(model)
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\lib\site-packages\django\contrib\admin\options.py", line 153, in check
    return cls.checks_class().check(cls, model, **kwargs)
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\lib\site-packages\django\contrib\admin\checks.py", line 497, in check
    errors.extend(self._check_list_filter(cls, model))
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\lib\site-packages\django\contrib\admin\checks.py", line 668, in _check_list_filter
    for index, item in enumerate(cls.list_filter)
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\lib\site-packages\django\contrib\admin\checks.py", line 713, in _check_list_filter_item
    get_fields_from_path(model, field)
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\lib\site-packages\django\contrib\admin\utils.py", line 457, in get_fields_from_path
    fields.append(parent._meta.get_field_by_name(piece)[0])
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\lib\site-packages\django\db\models\options.py", line 416, in get_field_by_name
    cache = self.init_name_map()
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\lib\site-packages\django\db\models\options.py", line 445, in init_name_map
    for f, model in self.get_all_related_m2m_objects_with_model():
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\lib\site-packages\django\db\models\options.py", line 563, in get_all_related_m2m_objects_with_model
    cache = self._fill_related_many_to_many_cache()
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\lib\site-packages\django\db\models\options.py", line 577, in _fill_related_many_to_many_cache
    for klass in self.apps.get_models():
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\lib\site-packages\django\utils\lru_cache.py", line 101, in wrapper
    result = user_function(*args, **kwds)
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\lib\site-packages\django\apps\registry.py", line 168, in get_models
    self.check_models_ready()
  File "C:\Users\PrImE\workspace\theindianpoll_virtualenv\lib\site-packages\django\apps\registry.py", line 131, in check_models_ready
    raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

问题模型:

class Question(models.Model):
    """
    """
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published', auto_now_add=True)

    def __unicode__(self):
        return self.question_text

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

    was_published_recently.admin_order_field = 'pub_date'
    was_published_recently.boolean = True
    was_published_recently.short_description = 'Published recently?'

选择模型:

class Choice(models.Model):
    """
    """
    question = models.ForeignKey(Question)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __unicode__(self):
        return self.choice_text

    class Meta:
        ordering = ['-votes']

如果我删除list_filter,一切正常。

1 个答案:

答案 0 :(得分:2)

您是在models.py或admin.py中注册模型吗? List_filter需要已经加载模型,因此您必须在admin.py中注册它,以便模型可以独立加载。