Thursday, January 27, 2011

Does anyone knows how to modify choices option of a fields from __init__(self,)?

Hi,

I have been trying to dynamically change CHOICES options of a
fields, which is modified inside the __init__(self,) of my model
class. I can see my choices being modified in the __init__(self,), BUT
using a modelform the final result is NOT considering the dynamic
changed CHOICES.

Does anyone knows how to modify choices option of a fields from
__init__(self,)?

Here is a simplified version of what I am trying to do...

----- models.py file content ----
CHOICES = [("default", "default"),]
class Profile(models.Model):
def __init__(self, *args, **kwargs):
super(Profile, self).__init__(*args, **kwargs)
dynamic_options = [("qc", "Quebec"), ("on", "Ontario")]
self._meta.get_field_by_name("state_province")[0]._choices
=dynamic_options

state_province = models.CharField(max_length=50, blank=True,
null=True, choices=CHOICES)


----- Forms.py file content -------
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile

The problem is that looking ProfileForm in a template....the
resulting dropdown of state_province will still
contain de default choices values and it is ignoring the dynamic
options set in __init__(self,)

how to fix this?

thanks,
Cocobuster

--
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


Real Estate