Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

two admin classes for one model django

I need to use one model in context of 2 admin classes. So, this is my model:

ITEM_STATUSES = (
('pending', _('Waiting approval')),
('approved', _('Approved')),
('declined', _('Declined'))
)

class Item(models.Model):
title = models.CharField(max_length=64)
description = models.TextField(blank=True)
...
status = models.CharField(max_length=32, choices=ITEM_STATUSES)
...

And I want to use it twice. First, I want to show all the models, like:

class ItemAdmin(admin.ModelAdmin):
  pass

admin.site.register(Item, ItemAdmin)

And also, I want a new page, where will be shown only models with status = 'pending', like that:

class ItemAdminPending(admin.ModelAdmin):
def queryset(self, request):
    qs = super(ItemAdminPending, self).queryset(request)
    return qs.filter(status='pending')

admin.site.register(Item, ItemAdminPending)

But of course I get an error: AlreadyRegistered: The model Item is already registered

Any suggestions? Hope to get help.

like image 649
Alex O Avatar asked Apr 03 '14 17:04

Alex O


People also ask

How do I restrict access to parts of Django admin?

Django admin allows access to users marked as is_staff=True . To disable a user from being able to access the admin, you should set is_staff=False . This holds true even if the user is a superuser. is_superuser=True .

Can Django admin be used in production?

Django's Admin is amazing. A built-in and fully functional interface that quickly gets in and allows data entry is priceless. Developers can focus on building additional functionality instead of creating dummy interfaces to interact with the database.

Can I use Django admin as frontend?

Django Admin is one of the most important tools of Django. It's a full-fledged application with all the utilities a developer need. Django Admin's task is to provide an interface to the admin of the web project. Django's Docs clearly state that Django Admin is not made for frontend work.

What is Inlines in Django admin?

The admin interface is also customizable in many ways. This post is going to focus on one such customization, something called inlines. When two Django models share a foreign key relation, inlines can be used to expose the related model on the parent model page. This can be extremely useful for many applications.


1 Answers

Very close! What you want is to add a proxy model in your models.py:

class ItemPending(Item):
    class Meta:
        proxy = True

And then register the second ModelAdmin like so:

admin.site.register(ItemPending, ItemAdminPending)
like image 170
frnhr Avatar answered Oct 24 '22 18:10

frnhr