Reputation: 1728
I have two fields in models.py
numberOfUsers = IntegerField()
numberOfUserCredits = IntegerField()
In Django admin, instead of viewing two columns with
list_display = ['numberOfUsers', 'numberOfUserCredits']
I'd like to instead clean this up a bit and concatenate these two fields for simpler viewing:
str(numberOfUsers) + '/' + str(numberOfUserCredits)
How can I create a custom field like this in Django admin? I could create a new field in models.py
that creates this field automatically on model save, but I am wondering if there is an easier way to go about doing this.
I found a similar question already asked, but the answer below was exactly what I was looking for.
Upvotes: 2
Views: 4479
Reputation: 15738
You can add it to your ModelAdmin class something like following, also i would suggest using f strings instead of concatenation
class SomeAdmin(admin.ModelAdmin):
list_display = (..., 'user_field')
def user_field(self, obj):
return f'{obj.numberOfUsers}/{obj.numberOfUserCredits}'
user_field.short_description = 'userShort'
Upvotes: 5