Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Does anyone knows how to modify choices option of a fields from __init__(self,)? [RSS Feed]

#1 Jan. 27, 2011 20:47:39

c.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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 =
class Profile(models.Model):
def __init__(self, *args, **kwargs):
super(Profile, self).__init__(*args, **kwargs)
dynamic_options =
self._meta.get_field_by_name("state_province")._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+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 27, 2011 21:18:55

Shawn M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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


In your __init__, after the super().__init__():

self.fields.choices = dynamic_options

Shawn

--
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+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Does anyone knows how to modify choices option of a fields from __init__(self,)? [RSS Feed]

Board footer

Moderator control

Enjoy the 24th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support