Django: Filtering or displaying a model method in Django Admin -
I have a model with a timeout DateField.
I want to install an administrator filter that will allow the user to toggle between "no expiration" and "any".
Model method is quite simple date comparison, no problem.
However this parameter or filter is not automatic in the admin farm.
Is such a thing possible, and if not, what would be an intelligent task ...
I would also be open to something like automatic extinction of deadlines , But I do not know how to start that path down.
After
You can enter two ModelAdmin
sections for this two times on the admin site Models can be included. You can override the query-based ()
method to ModelAdmin
to customize the code that has been shown. Note that you need to define a model proxy and use that in the second ModelAdmin
class, otherwise Django complains about registration twice the same model.
models.py
class ExampleModel (models.Model): has expired = models.DateField () Class ExpiredExampleModelProxy (ExampleModel): Square Meta: Proxy = True Verbose_name = 'expired example' verbose_name_plural = 'expired example'
admin.py
class NotExpiredExampleAdmin (models.ModelAdmin): def Queryset (self, requested): return (super (ExampleAdmin, self) .queryset (request) .filter (expiration__gte = date.today ()) class ExpiredExampleAdmin (models.ModelAdmin): def querygroup (self, requested): Return ( Super (ExampleAdmin, Self) .queryset (request) .filter (expiration__lt = date.toda ()) Admin.site.register (exampleModel, NotExpiredExampleAdmin) admin.site.register (timedoutEditModelProxix, ExpiredImport
ModelAdmin.queryset
Instead you can define the module's custom manager to get the same filtering out of the administrator too.
See also
Comments
Post a Comment