views:

35

answers:

1

Hi, I want to use django's admin filter on the list page. The models I have are something like this:

class Location(model):
  name = CharField()

class Inquiry(Model):
  name = CharFiled()
  location = ManyToManyField(Location)

Now I want to filter Inquiries, to display only those that contain relation to specific Location object. If I use

class InqAdmin(ModelAdmin):
  list_filter = ['location', ]

admin.site.register(Inquiry, InqAdmin)

the admin page displays me the list of all Locations and allows to filter.

What I would like to get, is to get list of only those locations that have some Inquiries in relation to them (so I don't ever get the empty list result after filtering).

How can this be done?

+1  A: 

You could create a custom manager for Locations that only returns Locations that have an Inquiry associated with them. If you make this the default manager, the admin will use it.

Only caveat is that you'll need create another manager that returns all Locations and use that in the rest of your app whenever you want to retrieve Locations that don't have an associated Inquiry.

The managers section in the Django docs is quite good, and should be all you need to get this set up.

Chris Lawlor