I store compressed strings in the database. I would like to see the actual string when in Django Admin. Since the strings are very long, I don't want them to appear in the list view, but only when viewing a single item (row). So, I can define:
@property
def short_my_field(self):
return pickle.loads(brotli.decompress(self.my_field))[:80]
@property
def original_my_field(self):
return pickle.loads(brotli.decompress(self.my_field))
I will then put short_my_field
into list_display
. But how do I cause original_my_field
to display when viewing a single item?
You can define a readonly_fields
as below,
class MyModelAdmin(admin.ModelAdmin):
readonly_fields = ("anything_you_want",)
fields = ("field_1", "anything_you_want")
def anything_you_want(self, obj):
return obj.original_my_field