change charfield choices from within a ModelForm
How do I change the CharField choices from within a ModelForm?
Here is the stripped down version of what I have:
-------------------------
class myModel(models.Model):
test = models.CharField(max_length=3,
verbose_name='test choices',
)
class myForm( ModelForm ):
class Meta:
model = myModel
-------------------------
One thing I tried was this but the choices didn't get added to the
form. Note: I haven't ran this version of the code.
-------------------------------
class myModel(models.Model):
test = models.CharField(max_length=3,
verbose_name='test choices',
)
def changeChoices(self, choiceTuple):
field = self._meta.get_field_by_name( 'test' )
[0]
field._choices = choiceTuple
class myForm( ModelForm ):
def __init__(self, *args, **kwargs):
super(myForm, self).__init__(*args, **kwargs)
choices = (
('a', 'choice a'),
('b', 'choice b'),
)
self.instance.changeChoices(choices)
class Meta:
model =
myModel
------------------------------
One thing I thought about was passing a parameter to the model init
but I'm not sure how to do that. I found this article that would seem
to work if I could figure out how to send params to the model's init:
http://blog.yawd.eu/2011/allow-lazy-dynamic-choices-djangos-model-fields/
Another thing I thought about was modifying the field in the form. In
the forms init do something like self.fields[ 'test' ]... but I'm not
sure where to access the choices.
Brian
--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home