Good stuff.

I take it that not specifying queryset on the ModelChoiceField
constructor doesn't
work?  If you really need an empty one by default, then the none() method seems
ideal.  The documentation says that this provides an EmptyQuerySet, so you don't
have to worry about it changing.  There's no database interaction
involved.  What
bothers you about it?

Bill

On Tue, Mar 2, 2010 at 11:00 AM, AlienBaby <matt.j.war...@gmail.com> wrote:
> Hi, thanks for the pointers,  I followed through Bill's suggestion,
> and while I could understand the approach I did indeed have problems
> with ''access to data from the request'' as this was all happening in
> the models.py in a class definition. But, reading around it and a
> little more google I ended up with the following,
>
> class aForm(ModelForm):
>        something=forms.ModelChoiceField(queryset=User.objects.none())
>
>        def __init__(self, *args, **kwargs):
>                queryset=kwargs.pop('queryset',None)
>                super(aForm, self).__init__(*args, **kwargs)
>                if queryset!=None:
>                        self.fields["something"].queryset = queryset
>
>        class Meta:
>              model=themodel
>
> Then, I can restrict the choices available from a view with,
>
> someform=aForm(queryset=somemodel.objects.filter(associated_user=request.user))
>
>
> This work fine, though I dont feel so good about passing
> User.objects.none() - is there a 'proper' way to get an EmptyQuerySet?
>
>
> Thanks,
> matt.
>
>
> On Mar 2, 4:28 pm, Alex Robbins <alexander.j.robb...@gmail.com> wrote:
>> If you set limit_choices_to on the underlying foreign key, I think
>> that shows up in any modelform derived from it too.
>>
>> http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.mod...
>>
>> Hope that helps,
>> Alex
>>
>> On Mar 1, 8:24 am, AlienBaby <matt.j.war...@gmail.com> wrote:
>>
>> > Hi.
>>
>> > I have a situation very similar to the following;
>>
>> > [code]
>>
>> > class chars(models.Model):
>> >          name=moels.CharField('Name',max_length=32)
>>
>> > associated_id=models.IntegerField('Associated',blank=False,null=False)
>> >          associated_id.default=0
>>
>> > class ctor(models.Model):
>> >           A=models.ForeignKey(chars,blank=False,null=False)
>> >           B=models.CharField('Text',max_length=512)
>>
>> > [/code]
>>
>> > I would like to generate a form using ModelForm. I started with
>>
>> > [code]
>>
>> > class ctorForm(ModelForm):
>> >     class Meta:
>> >         model=ctor
>>
>> > [/code]
>>
>> > This generates a form, but when rendered the A field of ctor becomes a
>> > selectable list of all entries in chars.
>>
>> > I would like the A field of the ctorForm to be a selectable list of
>> > only the entries in chars whose associated_id matches the ID of the
>> > currently logged in user. (login sessions are handled by django auth,
>> > or django authopenid)
>>
>> > I'd like to stick to creating the form using ModelForm if it's
>> > possible.
>>
>> > cheers,
>>
>>
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-us...@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-us...@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