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 automat开发者_开发知识库ed 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 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
- ticket 10712
- James Bennett's Xmas 2008 blog post
精彩评论