导入UserAdmin引发AppRegistryNotReady("模型尚未加载。")

时间:2015-03-08 12:06:08

标签: python django django-models django-admin

因此,我尝试使用OneToOneField扩展用户模型,但也修改UserAdmin以显示正确的信息,这是我在任何时候导入错误的原因

from django.contrib.auth.admin import UserAdmin

我收到此错误:

Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm3.4.1\helpers\pycharm\django_manage.py", line 23, in <module>
run_module(manage_file, None, '__main__', True)
File "C:\Python27\lib\runpy.py", line 176, in run_module
fname, loader, pkg_name)
File "C:\Python27\lib\runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File "C:\Python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Users\xXxKillDogxXx\Desktop\MinecraftMarket\manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\xXxKillDogxXx\AppData\Roaming\Python\Python27\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
utility.execute()
File "C:\Users\xXxKillDogxXx\AppData\Roaming\Python\Python27\site-packages\django\core\management\__init__.py", line 354, in execute
django.setup()
File "C:\Users\xXxKillDogxXx\AppData\Roaming\Python\Python27\site-packages\django\__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\xXxKillDogxXx\AppData\Roaming\Python\Python27\site-packages\django\apps\registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "C:\Users\xXxKillDogxXx\AppData\Roaming\Python\Python27\site-packages\django\apps\config.py", line 87, in create
module = import_module(entry)
File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "C:/Users/xXxKillDogxXx/Desktop/MinecraftMarket\market\__init__.py", line 2, in <module>
from django.contrib.auth.admin import UserAdmin
File "C:\Users\xXxKillDogxXx\AppData\Roaming\Python\Python27\site-packages\django\contrib\auth\admin.py", line 184, in <module>
admin.site.register(User, UserAdmin)
File "C:\Users\xXxKillDogxXx\AppData\Roaming\Python\Python27\site-packages\django\contrib\admin\sites.py", line 99, in register
admin_class.check(model)
File "C:\Users\xXxKillDogxXx\AppData\Roaming\Python\Python27\site-packages\django\contrib\admin\options.py", line 153, in check
return cls.checks_class().check(cls, model, **kwargs)
File "C:\Users\xXxKillDogxXx\AppData\Roaming\Python\Python27\site-packages\django\contrib\admin\checks.py", line 497, in check
errors.extend(self._check_list_filter(cls, model))
File "C:\Users\xXxKillDogxXx\AppData\Roaming\Python\Python27\site-packages\django\contrib\admin\checks.py", line 668, in _check_list_filter
for index, item in enumerate(cls.list_filter)
File "C:\Users\xXxKillDogxXx\AppData\Roaming\Python\Python27\site-packages\django\contrib\admin\checks.py", line 713, in _check_list_filter_item
get_fields_from_path(model, field)
File "C:\Users\xXxKillDogxXx\AppData\Roaming\Python\Python27\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\xXxKillDogxXx\AppData\Roaming\Python\Python27\site-packages\django\db\models\options.py", line 416, in get_field_by_name
cache = self.init_name_map()
File "C:\Users\xXxKillDogxXx\AppData\Roaming\Python\Python27\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\xXxKillDogxXx\AppData\Roaming\Python\Python27\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\xXxKillDogxXx\AppData\Roaming\Python\Python27\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\xXxKillDogxXx\AppData\Roaming\Python\Python27\site-packages\django\utils\lru_cache.py", line 101, in wrapper
result = user_function(*args, **kwds)
File "C:\Users\xXxKillDogxXx\AppData\Roaming\Python\Python27\site-packages\django\apps\registry.py", line 168, in get_models
self.check_models_ready()
File "C:\Users\xXxKillDogxXx\AppData\Roaming\Python\Python27\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.

以下是我尝试修改UserAdmin的文件,该文件是从Django Docs复制的:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from market.models import Market, MarketThemes, MarketServers, PremiumTransaction, PremiumUser, PremiumCodes, Themes, Templates, AdditionalUserSettings

class MarketAdmin(admin.ModelAdmin):
    search_fields = ['marketName', 'cName']
    list_display = ('owner', 'marketName', 'marketTheme', 'marketTemplates', 'subDomain', 'cName', 'currency', 'createdDate')


admin.site.register(Market,MarketAdmin)

class ThemeAdmin(admin.ModelAdmin):
    search_fields = ['title']
    list_display = ('owner', 'marketid', 'title', 'default')

admin.site.register(Themes,ThemeAdmin)

class ServersAdmin(admin.ModelAdmin):
    search_fields = ['serverName', 'apiKey']
    list_display = ('owner', 'marketid', 'serverName', 'apiKey')

admin.site.register(MarketServers,ServersAdmin)

admin.site.register(Templates)
admin.site.register(PremiumTransaction)
admin.site.register(PremiumUser)

class AdditionalUserSettingsInline(admin.StackedInline):
   model = AdditionalUserSettings
   can_delete=False


class CustomUserAdmin(UserAdmin):
   inlines = (AdditionalUserSettingsInline,)

admin.site.unregister(User) 
admin.site.register(User, CustomUserAdmin)

1 个答案:

答案 0 :(得分:1)

您应该在market/admin.py文件中定义管理员,而不是market/__init__.py