baconck
baconck

Reputation: 406

Django Formset will not filter by user objects

Can't figure out how to filter the formset to only show blogposts for the logged-in user. The forms renders fine, however it allows any user to assign a photo to any blogpost regardless of who owns the blogpost. How do I filter the options of blogposts to be for logged-in user only?


views.py

@login_required
def addimage(request):
    user = request.user
    blogposts = Blogpost.objects.filter(user=user)
    print blogposts
    imageformset = modelformset_factory(Image, AddImageForm,extra=1) 
    if request.method == "POST":
        formset = imageformset(request.POST, request.FILES)
        if formset.is_valid() :
            for form in formset.cleaned_data:
                    image = form['image']
                    title = form['title']
                    blogpost = form['blogpost']
                    description = form['description']
                    photo = Image(
                        title = title,
                        image= image,
                        blogpost=blogpost,
                        description=description,
                        )
                    photo.user = request.user
                    photo.save()

            messages.success(request, 'We did it. Pictures are on the interwebs!')
            return HttpResponseRedirect("/%s/%s/" % (user, blogpost.slug))


    else:
        formset = imageformset()

    return render_to_response('photos/add_photos.html', {
        'formset' : formset,
        'blogpost' : blogposts,
        },
        context_instance=RequestContext(request))

forms.py

from django import forms 
from .models import *

class AddImageForm(forms.ModelForm):
    class Meta:
        model   = Image
        fields = ('image', 'title', 'blogpost', 'description')

Add_photos.html

{% extends "base.html" %}
{% load static %}
{% block content %}
<div>
    <form action="" method="POST" enctype="multipart/form-data">{% csrf_token %}
    <div id="submit"><input id="submit-btn" type="submit" value="Save"></div>
        {{ formset.management_form }}
        {{ formset.as_p }}

       <input type="submit" value="Save">
    </form>
</div>
{% endblock %}

Upvotes: 1

Views: 583

Answers (1)

Paul Young
Paul Young

Reputation: 81

It looks like your blogpost field is a ForeignKey. By default this is represented by django.forms.ModelChoiceField. This inherits from ChoiceField whose choices are a model QuerySet. See the reference for ModelChoiceField.

So you want to provide a custom QuerySet to the field's queryset attribute. This depends a little on how your form is built. I find it easiest to do in the constructor but using the formset factory makes this a little tricky. You might try this after you construct the formset:

formset = imageformset()
for form in formset.forms:
    form.fields['blogpost'].queryset = Blogpost.objects.filter(user=user)

I think you'll also need to do that before you call formset.is_valid() in the request.method == 'POST' branch of the code.

Upvotes: 2

Related Questions