I’ve often seen examples like this in other projects’ code:
class Modela(models.Model):
abbr = models.CharField()
name = models.TextField()
class Admin:
list_display = ('abbr', 'name',)
Unfortunately, when I do this in my own code and then register the model with:
admin.site.register(Modela)
…the list_display variable is ignored and I get the output of __str__ for each entry in the database.
Do I really need to write separate ModelAdmin classes for each class, or is there a way to do it as above?
This syntax is from a very old version of Django – pre-‘newforms-admin’, which merged before 1.0 was released over three years ago. It doesn’t work any more, and there’s absolutely no reason to use it.
If you just want to change the
list_display, though, there isn’t any need to create classes: you can pass that into the register call: