[Fixed]-Django ForeignKey Works for Filter but not List Display

1👍

The issue isn’t with the id at all, it’s with get_veteran(). One of your plaques has no veteran, and thus raises an error. You should return a blank value (or something else) if there is no veteran:

def get_veteran(self, obj):
    if obj.veteran is not None:        
        return obj.veteran.first_name
    else:
        return ''

Note also that you can traverse relationships in list_display – I’m not 100% certain however that it won’t raise the same error if veteran is null though – looking through docs and my own code to find something similar to yours.

class PlaqueAdmin(admin.ModelAdmin):
    list_display = ['id', 'veteran__first_name', 'last_name', 'first_name', 'branch', 'group', 'draft']

Leave a comment