I have a model with an expiration DateField.
I want to set up an Admin filter that will allow the user to toggle between "Not Expired" and "Any".
The model method is quite a simple Date comparison, no problem.
However assigning this as a field or filter parameter in the AdminForm is not automatic.
Is such a thing possible, and if not, what would be a wise work-around...
I would even be open to some sort of automated deletion of Expired rows, but I don't know how to start down that path.
You could include the model twice on the admin site by registering two
ModelAdmin
classes for it. You can override thequeryset()
method of theModelAdmin
to customize which instances are shown. Note that you need to define a model proxy and use that in the secondModelAdmin
class, otherwise Django complains about registering the same model twice.models.py
admin.py
Instead of customizing
ModelAdmin.queryset
you could also define custom managers for the models to get the same filtering outside admin as well.See also