默认情况下,在Django-admin中有用户,组和站点应用程序。如何删除群组和网站?
我尝试从根网址中删除admin.autodiscover()
。然后,当我在我的应用模型中的某处添加admin.site.register(User, UserAdmin)
之类的内容时,我得到了AlreadyRegistered
异常(这是相当正确的 - 模型用户已在django.contrib.auth中注册)。
答案 0 :(得分:91)
在admin.py中你知道肯定会加载,试试:
admin.site.unregister(User)
admin.site.unregister(Group)
admin.site.unregister(Site)
答案 1 :(得分:9)
除上述内容外,请仔细检查" INSTALLED_APPS"在" settings.py"
INSTALLED_APPS = [
# django apps first
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# custom apps below
'my_app'
]
否则会导致错误。见这里:Issue with Django admin registering an inline user profile admin
答案 2 :(得分:5)
要删除我必须在admin.py中执行的用户和组:
from django.contrib import admin
# Need to import this since auth models get registered on import.
import django.contrib.auth.admin
import django.contrib.auth.models
from django.contrib import auth
admin.site.unregister(auth.models.User)
admin.site.unregister(auth.models.Group)
答案 3 :(得分:0)
如果您得到:
django.contrib.admin.sites.NotRegistered:模型组不是 已注册
然后确保您的INSTALLED_APPS顺序正确,如下所示:
enter code hereINSTALLED_APPS = (
# [1] Django apps
'django.contrib.auth',
...
# [2] your custom apps
'anyproject.anytuff',
)
答案 4 :(得分:0)
浏览所有应用程序,然后取消注册他们已注册的所有模型。
from django.apps import apps
# De-register all models from other apps
for app_config in apps.get_app_configs():
for model in app_config.get_models():
if admin.site.is_registered(model):
admin.site.unregister(model)
# Register only those models you want
...
答案 5 :(得分:0)