问题:Django管理员中同一模型的多个ModelAdmins /视图

如何为同一个模型创建一个以上的ModelAdmin,每个ModelAdmin进行不同的自定义并链接到不同的URL?

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

我知道我可以通过设置变量(例如list_display)或queryset在ModelAdmin中重写方法来以各种方式自定义页面上显示的对象列表:

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。但是我想拥有同一模型的多个视图/ ModelAdmins。例如,/admin/myapp/post将列出所有帖子对象,并/admin/myapp/myposts列出属于该用户的/admin/myapp/draftpost所有帖子,并可能列出尚未发布的所有帖子。(这些只是示例,我的实际用例更加复杂)

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

我看了看Django的源代码,发现ModelAdmin.changelist_view在urls.py中可以包含这样的函数,但是我不确定它是如何工作的。

更新:我找到了一种实现自己想要的方式(见下文),但是我仍然想听听其他实现方式。

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

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

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)

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)

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.

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.


回答 0

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

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

看完http://code.djangoproject.com/wiki/DynamicModels之后,我想出了以下函数实用程序函数来做同样的事情:

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)

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)

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.

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

This can be used as follows:

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

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

回答 1

保罗·斯通的回答绝对是伟大的!补充一下,对于Django 1.4.5,我需要继承自定义类admin.ModelAdmin

class MyPostAdmin(admin.ModelAdmin):
    def queryset(self, request):
        return self.model.objects.filter(id=1)

Paul Stone answer is absolutely great! Just to add, for Django 1.4.5 I needed to inherit my custom class from admin.ModelAdmin

class MyPostAdmin(admin.ModelAdmin):
    def queryset(self, request):
        return self.model.objects.filter(id=1)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。