* Rubic wrote, On 14.01.2007 18:19:
I've been using newforms for a few days now and just ran across something in ChoiceField that might be worth sharing.
However, sometimes you'd like the choice list to be dynamic, to reflect the current values in the model. My approach is to create a class with a flavors property attribute:
views.py: (version 2: dynamic choice list)
class FlavorChoice def _flavorList(self): return [(c.id, c.name) for c in Flavor.objects.all()] flavors = property(_flavorList)
class MyForm(forms.Form): flavor = forms.ChoiceField(label="Favorite Flavor", choices=FlavorChoice().flavors)
I don't know if there's a more idiomatic way to do this in newforms, but this approach is working for me.
Take a look at http://trac.studioquattro.biz/djangoutils/browser/branches/0.1/forms/fields/related.py (field) and http://trac.studioquattro.biz/djangoutils/browser/branches/0.1/forms/widgets/related.py (widget).
This is implementation of the RelatedSelect field (and widget) for single select related fields. It is still work in progress but I'm using it for some time now. -- Neboj�a or evi - nesh, ICQ#43799892 Studio Quattro - Ni� - Serbia http://studioquattro.biz/ | http://trac.studioquattro.biz/djangoutils/ Registered Linux User 282159 [http://counter.li.org] --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---