Skip to content

Commit

Permalink
Added “on” or “off” icons chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
shabda committed Feb 6, 2018
1 parent d93e7f0 commit 0189add
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
Binary file added docs/boolean_field_fixed.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions docs/boolean_fields.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
How to show “on” or “off” icons for calculated boolean fields?
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

In the previous chapter, :doc:`filtering_calculated_fields` you added a boolean field.::

def is_very_benevolent(self, obj):
return obj.benevolence_factor > 75

Which looks like this

.. image:: filter_calculated_fixed.png

The :code:`is_very_benevolent` field show the string `True` and `False`, unlike the builin BooleanFields which show an on and off indicator.
To fix this, you add a :code:`boolean` attribute on your method. You final modeladmin looks like this::

@admin.register(Hero)
class HeroAdmin(admin.ModelAdmin):
list_display = ("name", "is_immortal", "category", "origin", "is_very_benevolent")
list_filter = ("is_immortal", "category", "origin", IsVeryBenevolentFilter)

def is_very_benevolent(self, obj):
return obj.benevolence_factor > 75

is_very_benevolent.boolean = True

And your admin looks like this

.. image:: boolean_field_fixed.png
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Calculated fields
optimize_queries
sorting_calculated_fields
filtering_calculated_fields
boolean_fields

Permissions
+++++++++++++++++++++
Expand Down Expand Up @@ -81,7 +82,6 @@ Listview Page
increase_row_count
disable_pagination
date_based_filtering
boolean_fields
many_to_many

Changeview Page
Expand Down
2 changes: 2 additions & 0 deletions heroes_and_monsters/entities/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class HeroAdmin(admin.ModelAdmin):
def is_very_benevolent(self, obj):
return obj.benevolence_factor > 75

is_very_benevolent.boolean = True


@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
Expand Down

0 comments on commit 0189add

Please sign in to comment.