Ok, so here's my problem. I'll try not to go round the houses too much.
I have a django model which I want to expose with an admin interface. It foreign keys to a user model. Something like (note ordering of other fields):
class SomeModel(models.Model):
user = models.ForeignKey(...)
other_field2 = models.IntegerField(...)
other_field1 = models.IntegerField(...)
other_field3 = models.IntegerField(...)
...
I also have an admin interface for this model which looks like this:
class SomeModelAdmin(admin.ModelAdmin):
@admin.display(description="User id")
def get_user_id(self, obj):
return obj.user.id
list_display = (
"get_user_id",
"other_field1",
"other_field2",
"other_field3",
)
readonly_fields = (
"get_user_id",
"other_field1",
"other_field2",
)
As far as I can see from the docs I can only use the @admin.display
decorator for list_display
and read_only_fields
. But what if I want to change the order that the fields are displayed in? In the above code they'll default to the order defined in the model (read only User id comes first, followed by other_field2 and other_field1 then finally an editable other_field3).
I was hoping I could do this by defining a fields
tuple or overriding the get_fields
method, but this won't work for fields that use @admin.display
. Any ideas?
Ah I was being blind, this is in the docs https://docs.djangoproject.com/en/4.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.fields. You do this by defining tuple of readonly_fields, including the admin.display methods and then using those readonly fields in the fields tuple. Eg:
readonly_fields = (
"get_user_id",
"other_field1",
"other_field2",
)
fields = (
"get_user_id",
"other_field1",
"other_field2",
"other_field3",
)