Django

Code

Ticket #3287 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

[patch] Model methods in the change list can have checkmark icons by decorating with boolean=True

Reported by: xian@mintchaos.com Assigned to: adrian
Milestone: Component: django.contrib.admin
Version: Keywords: admin, list_display, boolean, checkmarks
Cc: Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Model methods that return true or false show up as 'True' or 'False' in the admin if you put them in the list_display. Which isn't anywhere near as nice as the checkmark icons that BooleanFields? get. If you want to get them yourself you have to do this:

    def is_current(self):
        from django.conf import settings
        current = self.start_date <= datetime.now() and self.end_date >= datetime.now()
        BOOLEAN_MAPPING = {True: 'yes', False: 'no', None: 'unknown'}
        result_repr = '<img src="%simg/admin/icon-%s.gif" alt="%s" />' % (settings.ADMIN_MEDIA_PREFIX, BOOLEAN_MAPPING[current], current)
        return result_repr
    is_current.allow_tags = True

Which is messy and brittle.

This patch lets you simply decorate the method with boolean=True and it works just like a booleanField

    def is_current(self):
        return self.start_date <= datetime.now() and self.end_date >= datetime.now()
    is_current.boolean = True

This is my first patch, so please look it over and advise if there is any way the styling could be improved to be more pythonic, or more djangonic. :)

I couldn't find any current tests that cover items_for_result() so I wasn't sure how to add tests for this. I'm pretty sure I could add to an existing test set, but am unsure how to go about creating a full new one.

That said, the change is pretty trivial.

Attachments

add_admin_boolean_checkmarks_to_callables.diff (2.1 kB) - added by xian@mintchaos.com on 01/12/07 00:38:24.
admin_boolean_docs.diff (1.1 kB) - added by xian@mintchaos.com on 01/12/07 01:11:07.
diff to add boolean=True info to the model creation docs

Change History

01/12/07 00:38:24 changed by xian@mintchaos.com

  • attachment add_admin_boolean_checkmarks_to_callables.diff added.

01/12/07 01:11:07 changed by xian@mintchaos.com

  • attachment admin_boolean_docs.diff added.

diff to add boolean=True info to the model creation docs

01/12/07 13:40:06 changed by jacob

  • status changed from new to closed.
  • resolution set to fixed.

(In [4309]) Fixed #3287: method columns in the admin changelist can now have a boolean attribute that uses the clever little checkmark icons instead of the ugly "True" or "False" strings. Thanks for the patch, xian@mintchaos.com


Add/Change #3287 ([patch] Model methods in the change list can have checkmark icons by decorating with boolean=True)




Change Properties
Action