Ticket #13452: regroup-choices.diff

File regroup-choices.diff, 1.5 KB (added by shacker, 5 years ago)

Document workaround for using regroup with CHOICES

  • docs/ref/models/instances.txt

     
    515515More details on named URL patterns are in the :ref:`URL dispatch documentation
    516516<topics-http-urls>`.
    517517
     518.. _extra-instance-methods:
     519
    518520Extra instance methods
    519521======================
    520522
  • docs/ref/templates/builtins.txt

     
    818818
    819819    {% regroup people|dictsort:"gender" by gender as gender_list %}
    820820
     821Regroup and CHOICES fields
     822^^^^^^^^^^^^^^^^^^^^^^^^^^
     823
     824If the "gender" field on the model in this example is a CHOICES field
     825rather than a foreign key, ``{{ gender.grouper }}`` will display the
     826choice's keys, rather than its values. Normally one would correct this
     827by using the :ref:`get_FOO_display() <extra-instance-methods>` method
     828to return the value. However, this does not work as one might expect
     829after regroup has processed the data -- it will no longer be possible
     830to get at the value string. The solution is to apply
     831``get_FOO_display()`` against the *input* of regroup, rather than the
     832output. So call regroup like this instead::
     833
     834    {% regroup people by get_gender_display as gender_list %}
     835
     836``{{ gender.grouper }}`` will now display the value fields from the
     837CHOICES set rather than the keys.
     838
     839
    821840.. templatetag:: spaceless
    822841
    823842spaceless
Back to Top