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.
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 .
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With