How to properly add a custom field as a callable to Django admin change form?

I’m trying to add a custom field (pretty_user) to a model’s admin page that displays the user info in a nice way. I’m adding the model as a callable directly in the ModelAdmin class. This works beautifully in the list_display view but it totally crashes in the change_form view.

class Model1Admin(admin.ModelAdmin):
    readonly_fields = [
        'id', 'configuration', 'ip_address', 'downloads', 'user', 'downloaded']

    list_display = ['id','downloads','pretty_user','downloaded',]

    def pretty_user(self, obj):
        return f"{obj.user.get_full_name()} - ({})"
    pretty_user.short_description = 'User'

    def get_fields(self, request, obj=None):
        fields = ('id', 'downloads', 'pretty_user', 'downloaded')
        return fields

Error traceback:

Unknown field(s) (pretty_user) specified for Model1. Check fields/fieldsets/exclude attributes of class Model1Admin.

The thing is that I have the same structure of adding custom fields as callables in the ModelAdmin for other models and they all work flawlessly. Here’s an example of one where I add render_logo as a custom field:

class Model2Admin(admin.ModelAdmin):
    list_display = ('id', 'name', 'name_long', 'created', 'updated', 'render_logo')
    readonly_fields = ['id', 'created', 'updated', 'render_logo']

    def get_fields(self, obj):
        fields = ('render_logo', 'id', 'name', 'name_long', 'created', 'updated')
        return fields
    def render_logo(self, m: 'Model2'):
        if m.logo is not None:
            return format_html(f'<img src="data:image/png;base64,{m.logo}" />')
            return format_html('<span>No logo</span>')

I have tried everything to make it work but I can’t see the problem. Any ideas?


render_logo custom_fields works because you make this field as readonly_fields.

If you make pretty_user field as a readonly_field then it also works.

readonly_fields = ['id', 'configuration', 'ip_address', 'downloads', 'user','downloaded', 'pretty_user']

otherwise you create a modelform where you can add any custom fields.