djangodjango-adminmany-to-manydjango-3.1

Django admin list_display is not displaying model method return item


I'm not able to get the list_display of admin to show the information from a model method, but the same method displays fine in the fieldsets on the change page. I think I've fixed all my mistakes that are present in other questions/answers on this topic, but I just can't seem to figure out what I've done. Any help will be much appreciated.

Admin:

class StudentAdminConfig(admin.ModelAdmin):
    list_dispaly = ('name', 'get_total_funds')
    fieldsets=(
        (None, {'fields': ('name', 'get_total_funds')}),
    )
    readonly_fields = ('get_total_funds',)

admin.site.register(Student, StudentAdminConfig)

Models: (Note: there is another model FundsEvent with a many-to-many relationship to Student)

class Student(models.Model):

    name = models.CharField(max_length=255)

    def get_total_funds(self):
        events = self.get_funds_events()
        total = float(0)
        for event in events:
            if event.income:
                total += float(event.amount)
            else:
                total -= float(event.amount)
        return f'${total:.2f}'
    get_total_funds.short_description = "Total Funds"

    def get_funds_events(self):
        return FundsEvent.objects.filter(students_attended=self)

    def __str__(self):
        return self.name

The annoying thing about this list_display issue is that it's not giving any errors I can traceback, it's just displaying the page with no column for the "Total Funds". Also, I've sunk so much time into this and it's not even that vital of a feature, just a convenience for me, as I'm literally the only one who will ever look at this display on this particular project, but I'm invested enough that I really want to figure it out, haha.

Again, thanks in advance.


Solution

  • What happens if you fix the typo in

    class StudentAdminConfig(admin.ModelAdmin):
        list_dispaly = ('name', 'get_total_funds')
        fieldsets=(
            (None, {'fields': ('name', 'get_total_funds')}),
        )
        readonly_fields = ('get_total_funds',)
    
    admin.site.register(Student, StudentAdminConfig)
    

    to make it:

    class StudentAdminConfig(admin.ModelAdmin):
        list_display = ('name', 'get_total_funds')
        fieldsets=(
            (None, {'fields': ('name', 'get_total_funds')}),
        )
        readonly_fields = ('get_total_funds',)
    
    admin.site.register(Student, StudentAdminConfig)