Login

Snippets by mauro

Snippet List

CustomChoiceField, Selectable label field version of ModelChoiceField

**The problem** ModelChoiceField always uses __unicode__ or __str__ to fill the labels. I needed to dynamically select which field to use for the labels. **The solution** My approach copies a lot from [this blog](http://oebfare.com/blog/2008/feb/23/overriding-modelchoicefield-labels/) with some modifications to make it more dynamic. There are some proposals to fix on this [Ticket #4620]( http://code.djangoproject.com/ticket/4620) **How to use** Include the code on your forms.py (or whatever you are using) and use the CustomChoiceField with the extra argument label_field instead of ModelChoiceField. Hope it helps someone.

  • newforms
  • forms
  • modelchoicefield
  • modelform
Read More

mauro has posted 1 snippet.