Opened 2 years ago

Closed 2 years ago

#33398 closed Cleanup/optimization (fixed)

ModelAdmin.empty_value_display example in docs should use list_display.

Reported by: Michael Owned by: Michael
Component: Documentation Version: 4.0
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Firstly, the documentation is great. In the admin site docs, it has this example:

from django.contrib import admin

class AuthorAdmin(admin.ModelAdmin):
    fields = ('name', 'title', 'view_birth_date')

    @admin.display(empty_value='???')
    def view_birth_date(self, obj):
        return obj.birth_date]

I think this will only work for the list display, not setting the empty value of the field during edit view.
Hence I think

fields = ('name', 'title', 'view_birth_date')

should become

list_display = ('name', 'title', 'view_birth_date')

Change History (5)

comment:1 by Mariusz Felisiak, 2 years ago

Summary: ModelAdmin.empty_value_display documentationModelAdmin.empty_value_display example in docs should use list_display.
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

I think this will only work for the list display, not setting the empty value of the field during edit view.

It also works for readonly_fields, but I agree that list_display fits better here. Would you like to provide a patch?

comment:2 by Michael, 2 years ago

Sure I will create a patch, before I submit it, would you please review the following, because I am not 100% sure I understand how the readonly functionality works for the empty_value:

Change from:

    You can also override ``empty_value_display`` for all admin pages with
    :attr:`AdminSite.empty_value_display`, or for specific fields like this::

        from django.contrib import admin

        class AuthorAdmin(admin.ModelAdmin):
            fields = ('name', 'title', 'view_birth_date')

            @admin.display(empty_value='???')
            def view_birth_date(self, obj):
                return obj.birth_date

To:

    You can also override ``empty_value_display`` for all admin pages with
    :attr:`AdminSite.empty_value_display`.
    
    To override just a specific ``list_display`` field, or one of the
    ``readonly_fields``, one can do so like this::

        from django.contrib import admin

        class AuthorAdmin(admin.ModelAdmin):
            list_display = ('name', 'title', 'view_birth_date')
            readonly_fields = ('view_birth_date', )
            fields = ('name', 'title', 'view_birth_date')

            @admin.display(empty_value='???')
            def view_birth_date(self, obj):
                return obj.birth_date

comment:3 by Mariusz Felisiak, 2 years ago

IMO it's not important to mention readonly_fields in ModelAdmin.empty_value_display docs, it's already documented in the display() decorator. I would only change fields to list_display:

  • docs/ref/contrib/admin/index.txt

    diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt
    index 7b97ee5638..4449afaaa6 100644
    a b subclass::  
    249249        from django.contrib import admin
    250250
    251251        class AuthorAdmin(admin.ModelAdmin):
    252             fields = ('name', 'title', 'view_birth_date')
     252            list_display = ('name', 'title', 'view_birth_date')
    253253
    254254            @admin.display(empty_value='???')
    255255            def view_birth_date(self, obj):

comment:4 by Mariusz Felisiak, 2 years ago

Has patch: set
Owner: changed from nobody to Michael
Status: newassigned
Triage Stage: AcceptedReady for checkin

comment:5 by Mariusz Felisiak, 2 years ago

Resolution: fixed
Status: assignedclosed
Note: See TracTickets for help on using tickets.
Back to Top