1

I have a model and an admin model with a form that allows someone to enter a comment on a host that has an outage status of "Active". The comment form (within Admin) works properly where it shows all the hosts that are in the outage table, however I want to hide all the host from the outage table that have a status of "Resolved". I'm not finding a way on the django doc to do this. Is this possible within the admin page? Filter out results from a table based on the value of a column?

zvisofer
  • 1,346
  • 18
  • 41
JasonS
  • 161
  • 2
  • 14

2 Answers2

1

Sorry I misunderstood your post.

To filter the results set that is displayed in the admin you can override the queryset() method of the admin class.

Like so:

class ExampleAdmin(admin.ModelAdmin):
    def queryset(self, request):
        qs = super(ExampleAdmin, self).queryset(request)
        return qs.exclude(status='Resolved')

This will exclude any rows where the status is "Resolved" from your admin page.

See also this SO post

Community
  • 1
  • 1
Patrick
  • 189
  • 1
  • 6
  • Thanks man. That got me 90% to where I needed to be. To build on this a bit, how would this work if the table with the status field is a foreignkey to a different model in the admin section? – JasonS Feb 10 '14 at 21:16
  • Just amend the exclude query: qs.exclude(status__name="Resolved") (if your status table has a field called name) https://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships – Patrick Feb 11 '14 at 09:15
0

Use the list_filter attribute?

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter

Patrick
  • 189
  • 1
  • 6
  • Doesn't the list filter just change the the filter section of the admin page? I don't people to be able to filter them out, I don't want them to show at all. Something like "select * from table where status != 'Resolved'" – JasonS Feb 07 '14 at 18:34