Multiple ModelAdmins/views for same model in Django admin

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):
        return Post.objects.filter(author=requser.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.

Django admin with multiple inlines of the same model

I’m having a problem with prefixes of StackedInlines in admin interface. I’ll try to post all the necesary code. models.py (brief) ##### Base classes class BaseItem(models.Model): pass class BaseProvi

Django Admin: automatically generate multiple inlines with the same model

I’m writing my first Django app. I have the following database model: class Person(models.Model): first_name = models.CharField(max_length = 100) last_name = models.CharField(max_length = 100) class I

Getting the different model views to work for the same model in Django-admin

I am trying to get two model views for the same model in django-admin and i am refering to this:Multiple model views and seem to have some problem in getting it solved. Code: from models import Studen

extending django admin model

The default view for a model on the django admin page shows a list of items in that view, but only gives one field. How is it possible to show multiple fields in the list?

Django admin – Mixing multiple model inlines in single admin interface

I have a Django model layout that looks something like this: class Author(models.Model): name = models.CharField(max_length=50, unique=True) class Publication(models.Model): author = models.ForeignKey

Multiple model edition in django-admin

I have models: class Lang(models.Model): name = models.CharField() symbol = models.CharField() … class News(models.Model): … some fields … class NewsLang(models.Model): news = models.ForeignKey(

Django – multiple pluralization in admin model

I have googled this for very long time but with no results. I`m beginner to Django so I don’t know all features it have. But this problem is very important for client 🙁 Could you help me, please? So

Django, Overriding behavior Admin Model

in the django admin, in any app model, the diplay data (datagrid o view template, i dont know how call it), django show all the entry, but what happen if i want that django just show the entry with

Django ListView for same model in multiple URLs

I am trying to use a ListView on the same model for multiple URLs in Django. Specifically, I have a home page using ListView and Pagination for blog posts and I want to have an archive page using List

Django admin display multiple fields on the same line

I have created a model, it will automatically display all the fields from the model and display it on the admin page. Now, I have a problem, I would like to have two fields on the same line, to do thi

Answers

just use a list_filter or date_hierarchy.

date_hierarchy = 'pub_date'

list_filter = ['pub_date',]

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 MyPosts(Post):
    class Meta:
        proxy = True

class MyPostAdmin(PostAdmin):
    def 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 queryset(self, request):
        return self.model.objects.filter(user = request.user)

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

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)