-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
29 additions
and
1 deletion.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters