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