ModelAdmin list_filter field member values are serialized using __str__ and not __repr__
|Reported by:||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
When generating the filter URL query parameters corresponding to model members with choice fields and specified in
list_filter of ModelAdmin, Django uses the str method of each of the choices to generate the URL query representation of each.
The Django docs specifically note the str as being one which returns an informal human readable representation:
Django should not be assuming that the str value is unambiguous or machine readable. The python standard noted at http://docs.python.org/2/reference/datamodel.html regarding repr() is that repr should be the function used for outputting a machine representation of the object:
"If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment)."
The above described inconvenience means that I have to make the str method in the values that I assign to my model members machine readable, which is goes against both Django and Python recommendations and makes my code messier.
Change History (8)
comment:1 Changed 3 years ago by
|Patch needs improvement:||unset|
|Status:||new → closed|