Hi Bobby,

See 
http://adil.2scomplement.com/2008/05/django-add-choices-to-form-fields-on-runtime/

Try something like the following:

# Define your choices
LOCATION_CHOICES = ((1,'location1'),
                    (2,'location2'),
                    (3,'location3'),
                    (4,'location4'),
                   )

LOCATION_CHOICES_PUBLIC = ((1,'location1'),
                    (2,'location2'),
                    (3,'location3'),
                   )

Example 1
---------
class ProfileForm(forms.Form):
    class Meta:
        model = Profile

    locations = forms.ChoiceField(required=True)

    def __init__(self, *args, **kwargs):
        # This is a bit of a hack
        if 'user_type' in kwargs and kwargs['user_type'] == 'public':
            public = True
            # We do not want to pass our kwarg to super
            del kwargs['user_type']
        else:
            public = False

        super(ProfileForm, self).__init__(*args, **kwargs)
        
        if 'user_type' in kwargs and kwargs['user_type'] == 'public':
            self.fields['locations'].choices = LOCATION_CHOICES_PUBLIC
        else:
            self.fields['locations'].choices = LOCATION_CHOICES

In your module use
form = ProfileForm(user_type='public')
or
form = ProfileForm()

Example 2
---------
class ProfileForm(forms.Form):
    class Meta:
        model = Profile
    
    # the limited choices are our default
    locations = forms.ChoiceField(choices = LOCATION_CHOICES_PUBLIC, 
required=True)

In your module
form = ProfileForm()
if not public:
    form.fields['locations'].choices = LOCATION_CHOICES


Example 2 is a bit 'cleaner'/neater I think.

Regards
Chris

-----Original Message-----
From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On 
Behalf Of Bobby Roberts
Sent: 16 February 2011 15:14
To: Django users
Subject: Re: form select box how to (help needed)

i have no idea what this means.... is there an example anywhere?

On Feb 16, 12:43 am, Kenneth Gonsalves <law...@thenilgiris.com> wrote:
> On Tue, 2011-02-15 at 19:05 -0800, Bobby Roberts wrote:
> > I can't load it through the "CHOICES" parameter in my forms field...
> > how can I do this?
>
> override __init__ in your form and populate there
> --
> regards
> KGhttp://lawgon.livejournal.com
> Coimbatore LUG roxhttp://ilugcbe.techstud.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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

-- 
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 at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to