Django admin 中同一模型的多个 ModelAdmins/视图

2022-01-25 00:00:00 python django django-admin

问题描述

如何为同一个模型创建多个 ModelAdmin,每个模型都以不同方式定制并链接到不同的 URL?

How can I create more than one ModelAdmin for the same model, each customised differently and linked to different URLs?

假设我有一个名为 Posts 的 Django 模型.默认情况下,此模型的管理视图将列出所有 Post 对象.

Let's say I have a Django model called Posts. By default, the admin view of this model will list all Post objects.

我知道我可以通过设置诸如 list_display 之类的变量或覆盖我的 ModelAdmin 中的 queryset 方法,以各种方式自定义页面上显示的对象列表,如下所示:

I know I can customise the list of objects displayed on the page in various ways by setting variables like list_display or overriding the queryset method in my ModelAdmin like so:

class MyPostAdmin(admin.ModelAdmin):
    list_display = ('title', 'pub_date')

    def queryset(self, request):
        request_user = request.user
        return Post.objects.filter(author=request_user)

admin.site.register(MyPostAdmin, Post)

默认情况下,可以通过 URL /admin/myapp/post 访问.但是我想拥有同一个模型的多个视图/模型管理员.例如,/admin/myapp/post 将列出所有帖子对象,/admin/myapp/myposts 将列出属于用户的所有帖子,/admin/myapp/draftpost 可能会列出所有尚未发布的帖子.(这些只是示例,我的实际用例更复杂)

By default, this would be accessible at the URL /admin/myapp/post. However I would like to have multiple views/ModelAdmins of the same model. e.g /admin/myapp/post would list all post objects, and /admin/myapp/myposts would list all posts belonging to the user, and /admin/myapp/draftpost might list all posts that have not yet been published. (these are just examples, my actual use-case is more complex)

您不能为同一模型注册多个 ModelAdmin(这会导致 AlreadyRegistered 异常).理想情况下,我希望不将所有内容放入单个 ModelAdmin 类并编写我自己的urls"函数以根据 URL 返回不同的查询集.

You cannot register more than one ModelAdmin for the same model (this results in an AlreadyRegistered exception). Ideally I'd like to achieve this without putting everything into a single ModelAdmin class and writing my own 'urls' function to return a different queryset depending on the URL.

我查看了 Django 源代码,发现像 ModelAdmin.changelist_view 这样的函数可能会以某种方式包含在我的 urls.py 中,但我不确定它究竟是如何工作的.

I've had a look at the Django source and I see functions like ModelAdmin.changelist_view that could be somehow included in my urls.py, but I'm not sure exactly how that would work.

更新:我找到了一种做我想做的事的方法(见下文),但我仍然想听听其他方法.

Update: I've found one way of doing what I want (see below), but I'd still like to hear other ways of doing this.


解决方案

我找到了一种方法来实现我想要的,通过使用代理模型来解决每个模型只能注册一次的事实.

I've found one way to achieve what I want, by using proxy models to get around the fact that each model may be registered only once.

class PostAdmin(admin.ModelAdmin):
    list_display = ('title', 'pubdate','user')

class MyPost(Post):
    class Meta:
        proxy = True

class MyPostAdmin(PostAdmin):
    def get_queryset(self, request):
        return self.model.objects.filter(user = request.user)


admin.site.register(Post, PostAdmin)
admin.site.register(MyPost, MyPostAdmin)

然后默认的 PostAdmin 可以在 /admin/myapp/post 访问,用户拥有的帖子列表将在 /admin/myapp/myposts.

Then the default PostAdmin would be accessible at /admin/myapp/post and the list of posts owned by the user would be at /admin/myapp/myposts.

查看http://code.djangoproject.com/wiki/DynamicModels后,我'已经想出了以下功能实用功能来做同样的事情:

After looking at http://code.djangoproject.com/wiki/DynamicModels, I've come up with the following function utility function to do the same thing:

def create_modeladmin(modeladmin, model, name = None):
    class  Meta:
        proxy = True
        app_label = model._meta.app_label

    attrs = {'__module__': '', 'Meta': Meta}

    newmodel = type(name, (model,), attrs)

    admin.site.register(newmodel, modeladmin)
    return modeladmin

可以这样使用:

class MyPostAdmin(PostAdmin):
    def get_queryset(self, request):
        return self.model.objects.filter(user = request.user)

create_modeladmin(MyPostAdmin, name='my-posts', model=Post)

相关文章