• <tfoot id='35QrL'></tfoot>
    <i id='35QrL'><tr id='35QrL'><dt id='35QrL'><q id='35QrL'><span id='35QrL'><b id='35QrL'><form id='35QrL'><ins id='35QrL'></ins><ul id='35QrL'></ul><sub id='35QrL'></sub></form><legend id='35QrL'></legend><bdo id='35QrL'><pre id='35QrL'><center id='35QrL'></center></pre></bdo></b><th id='35QrL'></th></span></q></dt></tr></i><div id='35QrL'><tfoot id='35QrL'></tfoot><dl id='35QrL'><fieldset id='35QrL'></fieldset></dl></div>
      <bdo id='35QrL'></bdo><ul id='35QrL'></ul>

    <small id='35QrL'></small><noframes id='35QrL'>

        <legend id='35QrL'><style id='35QrL'><dir id='35QrL'><q id='35QrL'></q></dir></style></legend>

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

        时间:2023-11-08
        <tfoot id='sR6Q1'></tfoot>
      1. <legend id='sR6Q1'><style id='sR6Q1'><dir id='sR6Q1'><q id='sR6Q1'></q></dir></style></legend>
          <tbody id='sR6Q1'></tbody>
            <i id='sR6Q1'><tr id='sR6Q1'><dt id='sR6Q1'><q id='sR6Q1'><span id='sR6Q1'><b id='sR6Q1'><form id='sR6Q1'><ins id='sR6Q1'></ins><ul id='sR6Q1'></ul><sub id='sR6Q1'></sub></form><legend id='sR6Q1'></legend><bdo id='sR6Q1'><pre id='sR6Q1'><center id='sR6Q1'></center></pre></bdo></b><th id='sR6Q1'></th></span></q></dt></tr></i><div id='sR6Q1'><tfoot id='sR6Q1'></tfoot><dl id='sR6Q1'><fieldset id='sR6Q1'></fieldset></dl></div>

            • <small id='sR6Q1'></small><noframes id='sR6Q1'>

                <bdo id='sR6Q1'></bdo><ul id='sR6Q1'></ul>

                  本文介绍了Django admin 中同一模型的多个 ModelAdmins/视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何为同一个模型创建多个 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)
                  

                  这篇关于Django admin 中同一模型的多个 ModelAdmins/视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:可以“list_display"在 Django ModelAdmin 中显示 ForeignKey 字段的属 下一篇:Django 1.3 或更低版本的 Django Admin 中的自定义过滤器

                  相关文章

                  <small id='hCY8w'></small><noframes id='hCY8w'>

                    1. <i id='hCY8w'><tr id='hCY8w'><dt id='hCY8w'><q id='hCY8w'><span id='hCY8w'><b id='hCY8w'><form id='hCY8w'><ins id='hCY8w'></ins><ul id='hCY8w'></ul><sub id='hCY8w'></sub></form><legend id='hCY8w'></legend><bdo id='hCY8w'><pre id='hCY8w'><center id='hCY8w'></center></pre></bdo></b><th id='hCY8w'></th></span></q></dt></tr></i><div id='hCY8w'><tfoot id='hCY8w'></tfoot><dl id='hCY8w'><fieldset id='hCY8w'></fieldset></dl></div>
                    2. <tfoot id='hCY8w'></tfoot>

                      <legend id='hCY8w'><style id='hCY8w'><dir id='hCY8w'><q id='hCY8w'></q></dir></style></legend>
                        <bdo id='hCY8w'></bdo><ul id='hCY8w'></ul>