[Fixed]-How do I call a model method in django ModelAdmin fieldsets?

30👍

Like that (from memory):

class AddressAdmin(admin.ModelAdmin):
    fieldsets = [(label, {'fields': ['address','address_2_html']}),]
    readonly_fields = ['address_2_html']

    def address_2_html(self, obj):
        return obj.address_2_html()
    address_2_html.allow_tags = True
    address_2_html.short_description = 'Address display'

2👍

Problem solved by overriding get_fieldsets() since the get_fieldsets() method allows access to the model object Address.

def get_fieldsets(self, request, obj=None):
    fs = [
        (self.label,  {'fields': ['address',]}),
        ('Map', {'fields': [], # required by django admin
                'description':obj.address_2_html(),
         }),
    ]
    return fs

Leave a comment