Opened 15 years ago

Closed 15 years ago

#11965 closed (wontfix)

A ModelAdmin method returning bool is not formatted with an image

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

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 Roman 15 years ago.

Download all attachments as: .zip

Change History (4)

by Roman, 15 years ago

Attachment: patch_v1.diff added

comment:1 by Ramiro Morales, 15 years ago

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 by Roman, 15 years ago

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 by Luke Plant, 15 years ago

Resolution: wontfix
Status: newclosed

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.

Note: See TracTickets for help on using tickets.
Back to Top