Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple ModelAdmins/views for same model in Django admin
    text
    copied!<p>How can I create more than one ModelAdmin for the same model, each customised differently and linked to different URLs?</p> <p>Let's say I have a Django model called Posts. By default, the admin view of this model will list all Post objects.</p> <p>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 <code>queryset</code> method in my ModelAdmin like so:</p> <pre><code>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) </code></pre> <p>By default, this would be accessible at the URL <code>/admin/myapp/post</code>. However I would like to have multiple views/ModelAdmins of the same model. e.g <code>/admin/myapp/post</code> would list all post objects, and <code>/admin/myapp/myposts</code> would list all posts belonging to the user, and <code>/admin/myapp/draftpost</code> might list all posts that have not yet been published. (these are just examples, my actual use-case is more complex)</p> <p>You cannot register more than one ModelAdmin for the same model (this results in an <code>AlreadyRegistered</code> exception). Ideally I'd like to achieve this <strong>without</strong> putting everything into a single ModelAdmin class and writing my own 'urls' function to return a different queryset depending on the URL.</p> <p>I've had a look at the Django source and I see functions like <code>ModelAdmin.changelist_view</code> that could be somehow included in my urls.py, but I'm not sure exactly how that would work.</p> <p><strong>Update</strong>: I've found one way of doing what I want (see below), but I'd still like to hear other ways of doing this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload