The Django admin docs says that it is possible to specify a callable as a value that can be used in list_display. If I need to pass some extra context to the function via function arguments, what’s the best way to accomplish that?
In pseudo code, what I’d like to do is something like:
App realestate:
models.py:
class A(models.Model):
raw = models.TextField()
admin.py:
from utils import processing
list_display = [processing('realestate app result', True)]
App party:
models.py:
class Person(models.Model):
raw = models.TextField()
admin.py:
from utils import processing
list_display = [processing('party app result', False)]
utils.py:
def processing(obj, short_description, allow_tags=False):
def process(obj):
# do something
pass
process.short_description = short_description
process.allow_tags = allow_tags
return process(obj)
You should not specify the
short_descriptionandallow_tagsinside the function itself.define processing like this:
utils.py:
on each ModelAdmin class, do:
So you have one place (utils.py) which contains the logic.
And every class defines the properties to display in the table.