martsberger / django-pivot

A module for pivoting Django Querysets
MIT License
209 stars 16 forks source link

get_FOO_display for ChoiceFields #1

Closed jangeador closed 6 years ago

jangeador commented 6 years ago

Is there any way to make the field display the get_foo_display value for choices fields? Right now I am having to do this:

qs = self.get_queryset().annotate(
            performance_level=Case(
                When(performance='1', then=Value('MP')),
                When(performance='2', then=Value('PP')),
                When(performance='3', then=Value('P')),
                When(performance='4', then=Value('HP')),
                default=Value('blank'),
                output_field=CharField(),
            ))

where the values MP, PP, P, and HP already live in the Choices tuple.

martsberger commented 6 years ago

Thanks for your input. I just pushed version 1.3 to PyPI which I think addresses this issue. If you pivot (or slice a histogram) on a field that has choices defined, the columns will be labeled with the display value from the choices tuple instead of the value stored in the database.

jangeador commented 6 years ago

That worked very well. Thanks!

jangeador commented 6 years ago

The row parameter (name of column that will key the rows) is not aware of the choices. This probably should know also if there are choices defined for it.

martsberger commented 6 years ago

Thanks, you're right about the row parameter. In version 1.6, now available on PyPI, if the row field has choices, the display value will be used as the key in the result.

jangeador commented 6 years ago

You are the man. Thanks.

Edit: just got a chance to test this. It is not working. Please refer to #3 for more info