Вопрос

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.

Это было полезно?

Решение

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)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top