views:

412

answers:

1

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.

+2  A: 

You could include the model twice on the admin site by registering two ModelAdmin classes for it. You can override the queryset() method of the ModelAdmin to customize which instances are shown. Note that you need to define a model proxy and use that in the second ModelAdmin class, otherwise Django complains about registering the same model twice.

models.py

class ExampleModel(models.Model):
    expired = models.DateField()

class ExpiredExampleModelProxy(ExampleModel):
    class Meta:
        proxy = True
        verbose_name = 'Expired Example'
        verbose_name_plural = 'Expired Examples'

admin.py

class NotExpiredExampleAdmin(models.ModelAdmin):
    def queryset(self, request):
        return (super(ExampleAdmin, self).queryset(request)
                .filter(expiration__gte=date.today()))

class ExpiredExampleAdmin(models.ModelAdmin):
    def queryset(self, request):
        return (super(ExampleAdmin, self).queryset(request)
                .filter(expiration__lt=date.today()))

admin.site.register(ExampleModel, NotExpiredExampleAdmin)
admin.site.register(ExpiredExampleModelProxy, ExpiredExampleAdmin)

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

akaihola
Brilliant. Thanks for your wisdom.
Jasconius