Question

I'm new to django and I need to show only those models in admin, which have status = 'pending'.

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

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

class ItemAdmin(admin.ModelAdmin):
  pass

admin.site.register(Item, ItemAdmin)
Was it helpful?

Solution

Override get_queryset() (for django>=1.6) method of your ModelAdmin:

The get_queryset method on a ModelAdmin returns a QuerySet of all model instances that can be edited by the admin site.

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

FYI, for django <= 1.5 use queryset() method instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top