从 Django-admin 中删除默认应用程序

发布于 2024-08-22 16:24:00 字数 267 浏览 4 评论 0原文

默认情况下,Django-admin 中有用户、组和站点应用程序。如何删除群组和网站?

我尝试从根网址中删除 admin.autodiscover() 。然后,当我在应用程序模型中的某处添加诸如 admin.site.register(User, UserAdmin) 之类的内容时,我得到了一个 AlreadyRegistered 异常(这是相当正确的 - 模型用户已经在 django.contrib.auth 中注册)。

By default, in Django-admin there is Users, Groups, and Sites apps. How can I remove Groups and Sites?

I tried to remove admin.autodiscover() from root urls. Then, when I added something like admin.site.register(User, UserAdmin) somewhere in my app models I got an AlreadyRegistered exception (this is fairly right - models users already registered in django.contrib.auth).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

海风掠过北极光 2024-08-29 16:24:00

在您知道肯定会加载的 admin.py 中,尝试:

admin.site.unregister(User)
admin.site.unregister(Group)
admin.site.unregister(Site)

In an admin.py you know will definitely be loaded, try:

admin.site.unregister(User)
admin.site.unregister(Group)
admin.site.unregister(Site)
向地狱狂奔 2024-08-29 16:24:00

除了上述之外,请仔细检查“settings.py”中“INSTALLED_APPS”的顺序,

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'
]

否则会导致错误。请参阅此处:Django 管理员注册内联用户配置文件时出现问题管理员

In addition to the above double check your ordering of "INSTALLED_APPS" in "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'
]

Otherwise it will cause an error. See here: Issue with Django admin registering an inline user profile admin

缺⑴份安定 2024-08-29 16:24:00

要摆脱用户和组,我必须在 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)

To get rid of Users and Groups I had to do in 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)
我ぃ本無心為│何有愛 2024-08-29 16:24:00

循环遍历所有应用程序,并取消注册它们已注册的任何模型。

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
...

Loop through all apps, and unregister any models they have registered.

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
...
始终不够爱げ你 2024-08-29 16:24:00

如果你有:

django.contrib.admin.sites.NotRegistered:模型组不是
已注册

然后确保您的 INSTALLED_APPS 按正确顺序排列,如下所示:

enter code hereINSTALLED_APPS = (
# [1] Django apps
'django.contrib.auth',
...

# [2] your custom apps
'anyproject.anytuff',
)

If you got:

django.contrib.admin.sites.NotRegistered: The model Group is not
registered

Then make sure that your INSTALLED_APPS in proper order like this:

enter code hereINSTALLED_APPS = (
# [1] Django apps
'django.contrib.auth',
...

# [2] your custom apps
'anyproject.anytuff',
)
女皇必胜 2024-08-29 16:24:00
from django.apps import apps

for model in apps.get_models():
    if model.__name__ and admin.site.is_registered(model):
        admin.site.unregister(model)

这将取消注册所有模型,具体取决于放置此代码的应用程序的位置以及它在 INSTALLED_APPS 内的顺序,因此请确保您想要在管理员中使用的应用程序放置在此代码所在的应用程序之后。

例如:如果此代码放在用户应用程序中,它将取消注册用户之前的所有模型以及用户可以注册之后的所有模型。

from django.apps import apps

for model in apps.get_models():
    if model.__name__ and admin.site.is_registered(model):
        admin.site.unregister(model)

this will unregister all models depending upon the position of app where this code is placed and it's order inside INSTALLED_APPS so make sure the apps you want in your admin are placed after the app in which this code resides.

For Example: if this code is placed inside users app, it will unregister all models before users and all models after users can be registered.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文