django-filter icon indicating copy to clipboard operation
django-filter copied to clipboard

Is it possible to control the `str` display of ModelMultipleChoiceFilter results?

Open shacker opened this issue 6 years ago • 3 comments
trafficstars

When using ModelMultipleChoiceFilter, the returned display value of each object in the returned queryset is the str() method of the model being queried. In most cases you can control this by altering the str() method of the queried model, but in our case we don't want to - we want to control it at the ModelMultipleChoiceFilter level.

Is this currently possible? If not, this is a feature request :)

shacker avatar Sep 06 '19 19:09 shacker

Filters use Django form fields under the hood, so this should be more a general question of "how do I modify the choice labels for a ModelMultipleChoiceField?"

You should be able to do this by implementing YourModel.label_from_instance.

rpkilby avatar Sep 06 '19 19:09 rpkilby

Aha! Excellent information, thanks. This might be a useful mention in the docs (though I understand it doesn't make sense to mention it in many places). Thanks!

shacker avatar Sep 06 '19 19:09 shacker

Yeah - one thing I'd like to clarify the docs is the relationship between filterset/form and filter/field.

rpkilby avatar Sep 06 '19 19:09 rpkilby