There’s photologue application, simple photo gallery for django, implementing Photo and Gallery objects.
Gallery object has ManyToMany field, which references Photo objects.
I need to be able to get list of all Photos for a given Gallery. Is it possible to add Gallery filter to Photo’s admin page?
If it’s possible, how to do it best?
You need to write a custom FilterSpec! Custom Filter in Django Admin on Django 1.3 or below
It’ll look like this:
Put it in a module
filters.pyin your app package and import it in youadmin.py(it’s important to import it, so that the filter becomes registered on the admin site!)EDIT: “f” is the field instance, in this case
models.ManyToManyFieldThe last line registers the FilterSpec for all fields that have a relation to the Gallery model. This will not work as you mentioned if the field is defined on the Gallery model, sincedjango.contrib.admin.views.main.ChangeList.get_filterschecks if the field you define in the list really exist on the model (doesnt work for related_name either). I think the easiest way around is that you could make a custom template for that changelist and hardcode your filter in there, the FilterSpec itself isn’t need for the filtering itself, django uses just the url get parameters for that!