[Fixed]-How to add admin custom field using a method

1👍

You should define colored_name inside PersonAdmin class not Person Model, because it’s a method used for the admin panel. Also, it receives an obj argument representing the person instance.

class PersonAdmin(admin.ModelAdmin):
    list_display = ('first_name', 'last_name', 'colored_name')

    def colored_name(self, obj):
        return format_html(
            '<span style="color: #{};">{} {}</span>',
            obj.color_code,
            obj.first_name,
            obj.last_name,
         )
👤levi

Leave a comment