Home > Net >  How to change the name of a field generated with a function in list_display in admin.py
How to change the name of a field generated with a function in list_display in admin.py

Time:11-13

i have a modelAdmin in admin.py. i want to display a value which i generated from a function in the model. here is the code:

admin.register(Salary)
class Salary(admin.ModelAdmin):
    list_display=('Category','BasicPA','BasicBM','Grade','Step','total')
    def BasicBM(self,instance):
        return instance.get_BasicBM()
    def total(self,instance):
        return instance.total()
    fieldsets =(
        ('Grade Salary Information',{
            'fields':('Category','BasicPA','RentP','TransportP','MealG','UtilityP','WeighInP','CSAP','TSAP','CallG','HazardG','RuralAllG','ShiftG','DomesticLawG','StateCounselG','Grade','Step')
            }),
        )

This works but it shows 'BasicBM' on the list_display title and i want to change it to 'Basic B/M'. i can't change it because i think the name of the string in list_display list has to match the name of the function in the class Salary in admin.py for this to work and the name of a function can't contain '/' or spaces. please, how do i achieve this? Thanks in Advance!

CodePudding user response:

Take a look at the display decorator. I've never used it but it seems to do what you want.

https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#django.contrib.admin.display

@admin.display(
    description='Basic B/M',
)
def BasicBM(self,instance):
        return instance.get_BasicBM()
  • Related