Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django filter the queryset of ModelChoiceField

Tags:

django

I'm been using a form field called ModelChoiceField and querying by all the objects but it's not extactly what I intended to use it for.

 class PictureForm(forms.ModelForm):
    Whiteboard = forms.ModelChoiceField(queryset=Whiteboard.objects.all())

I'm been trying to use the ModelChoiceField to query for all the WhiteBoard objects that belong to a particular user

Whiteboard = forms.ModelChoiceField(queryset=Whiteboard.objects.filter(user=request.user))

but I discovered that the request does not pass to the ModelForm .I'm been searching through SO for various solution and one solution was to override the forms's init()

This is probably the closest question that relate to my problem How to use the request in a ModelForm in Django

This was his solution .If his solution was to override the queryset at the views . How Does he create the ModelChoiceField without a queryset at the forms.py

In my case , I want filter all the whiteboards by users. How could I do it?

Parts of my module

class Whiteboard(models.Model):

    Category =models.CharField(max_length=30,choices=CATEGORY)
    user = models.ForeignKey(User)
    name = models.CharField(max_length=100)
    picture = models.OneToOneField('Picture',related_name='picture',blank=True,null=True)
    def __unicode__(self):
        return self.name

class Picture(models.Model):
    user = models.ForeignKey(User)
    Whiteboard = models.ForeignKey(Whiteboard,blank=False,null=False,related_name='board')
    image = models.FileField(upload_to="images/",blank=True)
    description = models.TextField()
    is_primary = models.BooleanField(default=False)

    def __unicode__(self):
        return self.description

My views.py

def PictureCreator(request):
if not request.user.is_authenticated():
    return HttpResponseRedirect(reverse('world:LoginRequest'))

if request.method == "POST":
    form = PictureForm(request.POST , request.FILES)
    if form.is_valid():
        picture = Picture(user=request.user)
        image = request.FILES.get('image')
        if image:
                            picture.image = form.cleaned_data['image']
                    description = form.cleaned_data['description']
        if description:
                            picture.description = form.cleaned_data['description']

        if board:
                            picture.board = form.cleaned_data['board']
        picture.save()
        board = Whiteboard.objects.get(Whiteboard=picture.board)
        the_id = board.id
    return HttpResponseRedirect(reverse('world:Boat', kwargs={'animal_id': the_id }))   

return render(request,'picture.html',{'form':PictureForm()})

picture.html

<form method="POST" enctype="multipart/form-data">
    {% csrf_token %}
{{ form.as_p }}
<input type = "submit" value= "Add Picture" />
 </form>
like image 307
donkeyboy72 Avatar asked Dec 04 '22 11:12

donkeyboy72


1 Answers

Do not set a filter at all during class definition. You'll override it in your view.

form = PictureForm()
form.fields['whiteboard'].queryset = Whiteboard.objects.filter(user=request.user)

class PictureForm(ModelForm):
   whiteboard = forms.ModelChoiceField(queryset=Whiteboard.objects.none(),)
like image 172
furins Avatar answered Dec 23 '22 22:12

furins