Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading