RE: form select box how to (help needed)

2011-02-16 Thread Chris Matthews
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.



Re: form select box how to (help needed)

2011-02-16 Thread Bobby Roberts
i have no idea what this means is there an example anywhere?

On Feb 16, 12:43 am, Kenneth Gonsalves  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.



Re: form select box how to (help needed)

2011-02-15 Thread Kenneth Gonsalves
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
KG
http://lawgon.livejournal.com
Coimbatore LUG rox
http://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.



form select box how to (help needed)

2011-02-15 Thread Bobby Roberts
I've got several select boxes in a user profile that the we can
manipulate in /admin.  One of these select boxes is called "locations"
and has locations to which you can assign a user.

for explanation, let's say that that the select box is populated like
such:


1,location1
2,location2
3,location3
4,location4

where the numbers are the option values and the text is the option
text.

In this user profile, if they choose ocation1,location2,location3, it
saves to a session variable as "1,2,3" (and of course also saves in
their profile.)

so far so good.


now here's what i need help with.

On the public side of the site, they'll login and do certain things.
i want to have a select box (as previously described), HOWEVER, i only
want it populated with options 1,2 and 3 (the values in their session
variable)... not the 4th location since they haven't been assigned to
that location.


I can't load it through the "CHOICES" parameter in my forms field...
how can I do this?

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