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


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'


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
    return fs

Leave a comment