django:检查给定模型的 modeladmin

发布于 2024-09-04 15:01:15 字数 109 浏览 2 评论 0原文

如何检查给定模型是否存在模型管理员?

modeladmins 是通过向 admin.site 对象注册模型来创建的。如何检查站点对象以查看已注册哪些模型以及哪个 admin_class?

How does one check to see if a modeladmin exists for a given model?

modeladmins are created by registering a model with the admin.site object. how can one check the site object to see which models have been registered, and with which admin_class?

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

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

发布评论

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

评论(2

谎言 2024-09-11 15:01:15

有趣的问题,这促使我做了一些挖掘。

一旦管理类被注册,它们就会被存储在 site 对象的一个​​属性中,这个属性被称为 - 毫不奇怪 - _registry。这是模型类到 modeladmin 类的字典 - 请注意,键和值都是类,而不是名称。

因此,如果您有这样的 admin.py:

from django.contrib import admin
from myapp.models import MyModel

class MyModelAdmin(admin.ModelAdmin):
    list_display = ('field1', 'field2')

admin.site.register(MyModel, MyModelAdmin)

那么一旦实际导入 - 通常是通过 urls.py 中的 admin.autodiscover() 行 - admin.site._registry 将包含类似这样的内容:

{<class 'myapp.models.MyModel'>: 
    <django.contrib.admin.options.ModelAdmin object at 0x10210ba50>}

并且您将通过使用模型本身作为键来获取 MyModel 的 ModelAdmin 对象:

>>> admin.site._registry[MyModel]
<django.contrib.admin.options.ModelAdmin object at 0x10210ba50>

Interesting question, which provoked me to do a little digging.

Once the admin classes have been registered, they are stored in an attribute of the site object called - not surprisingly - _registry. This is a dictionary of model classes to modeladmin classes - note both the keys and values are classes, not names.

So if you have an admin.py like this:

from django.contrib import admin
from myapp.models import MyModel

class MyModelAdmin(admin.ModelAdmin):
    list_display = ('field1', 'field2')

admin.site.register(MyModel, MyModelAdmin)

then once that has actually been imported - usually by the admin.autodiscover() line in urls.py - admin.site._registry will contain something like this:

{<class 'myapp.models.MyModel'>: 
    <django.contrib.admin.options.ModelAdmin object at 0x10210ba50>}

and you would get the ModelAdmin object for MyModel by using the model itself as the key:

>>> admin.site._registry[MyModel]
<django.contrib.admin.options.ModelAdmin object at 0x10210ba50>
随梦而飞# 2024-09-11 15:01:15

Django 的 django.contrib.admin.sites.AdminSite 有一个检查注册模型的方法,称为 .is_registered(model) 。此方法将检查管理网站的 _registry 属性(就像 Daniel Roseman 的方法)

所以,如果你有这样的文件:

# models.py

from django.db import models

class MyModel(models.Model)
    field1 = ...
    field2 = ...
# admin.py

from django.contrib import admin
from .models import MyModel

class MyModelAdmin(admin.ModelAdmin):
    list_display = ('field1', 'field2')

admin.site.register(MyModel, MyModelAdmin)

你可以做一些这样的测试:

# tests.py

from django.test import TestCase
from .models import MyModel

class TestModelAdmin(TestCase):

    def test_mymodel_registered(self):
        self.assertTrue(admin.site.is_registered(MyModel))

nb:我已经在 Django 1.8Django 2.2

Django's django.contrib.admin.sites.AdminSite has a method for checking registered Model called .is_registered(model). This method will check on the admin site's _registry attribute (just like Daniel Roseman's approach)

So, if you have files like these:

# models.py

from django.db import models

class MyModel(models.Model)
    field1 = ...
    field2 = ...
# admin.py

from django.contrib import admin
from .models import MyModel

class MyModelAdmin(admin.ModelAdmin):
    list_display = ('field1', 'field2')

admin.site.register(MyModel, MyModelAdmin)

You could make some test like this:

# tests.py

from django.test import TestCase
from .models import MyModel

class TestModelAdmin(TestCase):

    def test_mymodel_registered(self):
        self.assertTrue(admin.site.is_registered(MyModel))

nb: I've checked it on the Django's modules documentation from Django 1.8 to Django 2.2

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