简体   繁体   English

为什么需要在Django admin中同时注册应用名称和应用管理员名称?

[英]Why does one need to register both app name and app admin name in Django admin?

I am trying to understand why, when adding a custom Admin site for a django app, one needs to add both the model and the admin function. 我试图理解为什么在为Django应用添加自定义管理站点时,需要同时添加模型和管理功能。 Say, you have an application called Story , and thus the admin site will be called StoryAdmin . 假设您有一个名为Story的应用程序,因此管理站点将称为StoryAdmin When registering this on the django administration interface, you need to add this line: 在django管理界面上注册时,您需要添加以下行:

# Registering all the changes to admin.site
admin.site.register(Story, StoryAdmin)

My question is, is there a way to just do this: 我的问题是,有没有办法做到这一点:

admin.site.register(StoryAdmin)

Adding only one thing, not two, because this makes things simpler, and there is a smaller chance for an error, and the code just looks less redundant. 仅添加一件事,而不是添加两件事,因为这使事情变得更简单,并且出错的可能性也较小,并且代码看起来不那么冗余。 It would make things look much better, because in the end, you could have a clean list of all the admin panels: 这会使外观看起来好很多,因为最后,您可以得到所有管理面板的完整列表:

admin.site.register(
    StoryAdmin,
    SomeAdmin,
    FooAdmin,
)

That's not how admin.site.register is built . 那不是admin.site.register的构建方式 It expects a Model and then optionally the ModelAdmin with which to display that Model: 它需要一个模型,然后需要一个ModelAdmin来显示该模型:

def register(self, model_or_iterable, admin_class=None, **options):
    """
    Registers the given model(s) with the given admin class.

    The model(s) should be Model classes, not instances.

    If an admin class isn't given, it will use ModelAdmin (the default
    admin options). If keyword arguments are given -- e.g., list_display --
    they'll be applied as options to the admin class.

    If a model is already registered, this will raise AlreadyRegistered.

    If a model is abstract, this will raise ImproperlyConfigured.
    """

This allows you to use the same ModelAdmin on multiple Models (which can be desirable when, for example, you subclass a model off the same abstract Model). 这使您可以在多个模型上使用相同的ModelAdmin(例如,当您从同一抽象模型继承一个模型时,这可能是理想的)。

It doesn't suit your style but it's just one of those things you just have to accept and get on with. 它不适合您的风格,但这只是您必须接受并继续进行的事情之一。

That's a good question, it could have been designed that way, but that's not the case. 这是一个很好的问题,它本来可以这样设计,但事实并非如此。

I asume the main reason is to allow the admin class to be optional and use the default base ModelAdmin class for simple cases and to allow to use the same ModelAdmin subclass with many models , see implementation at: https://github.com/django/django/blob/master/django/contrib/admin/sites.py#L52-101 我认为主要原因是允许admin类是可选的,并在简单情况下使用默认的基本ModelAdmin类,并允许对许多模型使用相同的ModelAdmin子类 ,请参阅以下网址的实现: https : //github.com/django /django/blob/master/django/contrib/admin/sites.py#L52-101

Also you don't need to define the class yourself if you need to customize a ModelAdmin, you can just pass kwargs to the register function: 另外,如果您需要自定义ModelAdmin,则无需自己定义类,只需将kwargs传递给register函数即可:

admin.site.register(Story, list_display=['field1', 'field2'])

So that's my bet, they try to minimize the boilerplate when registering a model and allow it to be more flexible. 因此,我敢打赌,他们在注册模型时会尽量减少样板,并使其更加灵活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM