Code

Opened 5 years ago

Closed 5 years ago

#11965 closed (wontfix)

A ModelAdmin method returning bool is not formatted with an image

Reported by: romkyns Owned by: nobody
Component: contrib.admin Version: master
Severity: Keywords: list_display
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Consider the following model admin:

class AppleAdmin(admin.ModelAdmin):
    list_display = ('has_something',)

    def has_something(self, apple):
        return apple.something != None

The "has_something" method returns a bool. When viewing this model through the list of "apples" in the admin, the "has_something" column will currently simply say "True/False", whereas a boolean that's actually part of the "apple" model will be formatted with a special graphic.

Trivial patch attached.

Attachments (1)

patch_v1.diff (670 bytes) - added by romkyns 5 years ago.

Download all attachments as: .zip

Change History (4)

Changed 5 years ago by romkyns

comment:1 Changed 5 years ago by ramiro

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Just in case you haven't seen it, the currently way to not only achieve but also control this is documented (fifth item in the second bullted list):

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display

(namely, specifying a True value for the .boolean attribute for the boolean-returning method.)

Is there a reason you are proposing to expose second, more limited way to do to this?

comment:2 Changed 5 years ago by romkyns

You're right, I haven't seen it.

Still, it seems natural that if a boolean model field gets this styling automatically (i.e. without me specifying any extra properties anywhere) then so should a modeladmin method returning bool. Do you think such behaviour would be inappropriate?

comment:3 Changed 5 years ago by lukeplant

  • Resolution set to wontfix
  • Status changed from new to closed

Given we already have one way of controlling whether this behaviour occurs, I think it's inappropriate to automatically do it as well. The analogy to a boolean model field fails because you have specified very explicitly that the field is boolean by using BooleanField (or something), which is not the same as a method that happens to return a boolean value, but it could be anything.

Secondly, the autodetection fails if you have a method that happens to use 0 for False and 1 for True, which some (old-ish) Python libraries still do, and it also fails if you want 'None' to return the 'unknown' graphic (as it does for null boolean model fields).

These things considered, it's better to just keep a single, explicit method that always works.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.