List Info

Thread: newforms ModelChoiceField queryset




newforms ModelChoiceField queryset
user name
2007-11-06 06:05:18
Hello there,
		  I'm using Django 0.96 here. I've got a couple of models
related by a ForeignKey that look more or less like this:

 class Server(models.model):
     name = models.CharField(maxlength=20)
     users = models.ManyToManyField(User)

 class Ticket(models.model):
     desc = models.TextField()
     server = models.ForeignKey(Server)


 Then, I define a form to load tickets:

 from django import newforms as forms

 class NewTicket(forms.Form):
     desc = forms.CharField(widget=forms.Textarea())
     server =
forms.ModelChoiceField(queryset=Server.objects.all())

OK then, what I want to do is filter the servers in the form
according to the current user (request.user). The only way
I've been able to do this (so far) in a view is this:

	serversbyuser = Server.objects.filter(users=request.user)
	ticketform = NewTicket()
	ticketform.fields['server'] =
forms.ModelChoiceField(queryset=serverbyuser)

 The question is: why doesn't something like this work:

	ticketform.fields['server'].queryset =
Server.objects.filter(users=request.user)
?
 It doesn't end up on any error, but the rendered form
doesn't filter out the server choices.. any clue?

 Regards.

-- 
Gonzalo Delgado <gonzalodelgadofortix.com.ar>
Re: newforms ModelChoiceField queryset
country flaguser name
United States
2007-11-06 12:28:39
Gonzalo Delgado wrote:
>
>  The question is: why doesn't something like this
work:
>
> 	ticketform.fields['server'].queryset =
Server.objects.filter(users=request.user)
> ?
>  It doesn't end up on any error, but the rendered form
doesn't filter out the server choices.. any clue?
>
>   
Sounds like you're hitting bug 4787 
(http://code
.djangoproject.com/ticket/4787), where the change in 
queryset on the field doesn't get propagated to the widget.
You can work 
around by assigning to the widget's choices directly :
ticketform.fields['server'].widget.choices = 
ticketform.fields['server'].choices
(after changing the field's choices, obviously)

Chris


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: newforms ModelChoiceField queryset
user name
2007-11-06 13:12:11
EL TUE, 06 NOV 2007 10:28:39 -0800
CHRIS BRAND <CBRANDREDBACK.COM> ESCRIBIó:

> 
> GONZALO DELGADO WROTE:
> >
> >  THE QUESTION IS: WHY DOESN'T SOMETHING LIKE THIS
WORK:
> >
> > 	TICKETFORM.FIELDS['SERVER'].QUERYSET =
SERVER.OBJECTS.FILTER(USERS=REQUEST.USER)
> > ?
> >  IT DOESN'T END UP ON ANY ERROR, BUT THE RENDERED
FORM DOESN'T FILTER OUT THE SERVER CHOICES.. ANY CLUE?
> >
> >   
> SOUNDS LIKE YOU'RE HITTING BUG 4787 
> (HTTP://CODE.DJANGOPROJECT.COM/TICKET/4787), WHERE THE
CHANGE IN 
> QUERYSET ON THE FIELD DOESN'T GET PROPAGATED TO THE
WIDGET. YOU CAN WORK 
> AROUND BY ASSIGNING TO THE WIDGET'S CHOICES DIRECTLY :
> TICKETFORM.FIELDS['SERVER'].WIDGET.CHOICES = 
> TICKETFORM.FIELDS['SERVER'].CHOICES
> (AFTER CHANGING THE FIELD'S CHOICES, OBVIOUSLY)
> 
> CHRIS
> 

 SMELLED LIKE A BUG.. THANKS CHRIS! 

-- 
GONZALO DELGADO <GONZALODELGADOFORTIX.COM.AR>
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )