0

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.

Raleford
  • 3
  • 2

2 Answers2

1

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)
AMG
  • 1,606
  • 1
  • 14
  • 25
  • Oh gosh... I knew it would be something dumb. Does it help you to get this listed as the correct answer or is better overall for site cleanup to delete the question? Also, thank you. I spent way too much time on that. – Raleford Mar 09 '21 at 03:27
  • No worries, typos have bit everyone at some point! Thanks for the accepted answer. – AMG Mar 09 '21 at 03:59
  • don't delete the question please, I also have a similar question and without typos.. I need to look over this again. – Conor Jul 02 '22 at 00:52
0

it works for me..

from django.contrib import admin
from .models import Student
class StudentAdminConfig(admin.ModelAdmin):
    list_display = ['name', 'email', 'phone']

this should be with StudentAdminConfig

admin.site.register(Contact, StudentAdminConfig)
Avinash Raut
  • 1,872
  • 20
  • 26