martsberger / django-pivot

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

Choice Field on row display not working #3

Closed jangeador closed 6 years ago

jangeador commented 6 years ago

The fix for 1.6 that you described in #1 is not working. Here is a failing test showing the behavior. It says that the field (get_FOO_display()) is not found.

    def test_choice_field_on_row(self):
        pt = pivot(ShirtSales.objects.all(), 'gender', 'style', 'units')
        self.assertIsNotNone(pt)

I wish I knew how to fix it, but my knowledge of how your module works is limited. Otherwise I would be happy to send you a PR.

And this is the error itself when I run the test above:

$ python runtests.py --settings=django_pivot.tests.test_postgres_settings
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
E........
======================================================================
ERROR: test_choice_field_on_row (django_pivot.tests.pivot.test.Tests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/delio/Projects/django-pivot/django_pivot/tests/pivot/test.py", line 104, in test_choice_field_on_row
    pt = pivot(ShirtSales.objects.all(), 'gender', 'style', 'units')
  File "/Users/delio/Projects/django-pivot/django_pivot/pivot.py", line 30, in pivot
    queryset = queryset.annotate(row_display=row_display)
  File "/Users/delio/.virtualenvs/django_pivot/lib/python3.6/site-packages/django/db/models/query.py", line 945, in annotate
    clone.query.add_annotation(annotation, alias, is_summary=False)
  File "/Users/delio/.virtualenvs/django_pivot/lib/python3.6/site-packages/django/db/models/sql/query.py", line 973, in add_annotation
    summarize=is_summary)
  File "/Users/delio/.virtualenvs/django_pivot/lib/python3.6/site-packages/django/db/models/expressions.py", line 865, in resolve_expression
    c.cases[pos] = case.resolve_expression(query, allow_joins, reuse, summarize, for_save)
  File "/Users/delio/.virtualenvs/django_pivot/lib/python3.6/site-packages/django/db/models/expressions.py", line 799, in resolve_expression
    c.result = c.result.resolve_expression(query, allow_joins, reuse, summarize, for_save)
  File "/Users/delio/.virtualenvs/django_pivot/lib/python3.6/site-packages/django/db/models/expressions.py", line 471, in resolve_expression
    return query.resolve_ref(self.name, allow_joins, reuse, summarize)
  File "/Users/delio/.virtualenvs/django_pivot/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1477, in resolve_ref
    self.get_initial_alias(), reuse)
  File "/Users/delio/.virtualenvs/django_pivot/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1417, in setup_joins
    names, opts, allow_many, fail_on_missing=True)
  File "/Users/delio/.virtualenvs/django_pivot/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1352, in names_to_path
    "Choices are: %s" % (name, ", ".join(available)))
django.core.exceptions.FieldError: Cannot resolve keyword 'Boy' into field. Choices are: gender, id, price, shipped, store, store_id, style, units

----------------------------------------------------------------------
Ran 9 tests in 1.110s

FAILED (errors=1)
Destroying test database for alias 'default'...
martsberger commented 6 years ago

Thanks for catching this. I have fixed the error. The resulting pivot table will have two keys for the row foo if it is a choice field, 'foo' and 'get_foo_display', where the former has the raw database value and the latter has the display value.

Lesson learned about skimping on tests. :-)

jangeador commented 6 years ago

It works. Thanks.