On Thu, Jan 14, 2010 at 6:39 PM, GoSantoni <[email protected]> wrote:
> Hi
>
> Is it possible to use extra filter on a ModelChoiceField?
>
> Tried several strategies:
>
> if photo.member == user:
> url = forms.ModelChoiceField(queryset=Image.objects.filter())
>
> or
> url = forms.ModelChoiceField(queryset=Image.objects.filter
> (member__id=1))
>
> or
>
> if Image.objects.filter(member__id=1):
> url = forms.ModelChoiceField(queryset=Image.objects.filter
> (member__id=1))
>
> All fail to filter on the member / user
>
> Thanks
>
The technique I use to do forms like this is to add the queryset in
the constructor, passing in whatever I need to filter it.
Eg:
class AttachmentForm(forms.Form):
name = forms.CharField(max_length=32)
url = forms.ModelChoiceField(queryset=Image.objects.none())
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super(forms.Form, self).__init__(*args, **kwargs)
self.fields['url'].queryset = Image.objects.filter(user=self.user)
Cheers
Tom
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.