finspin
finspin

Reputation: 4061

Filter select field in ModelForm by currently logged in user

I'm trying to display a form (ModelForm) with a select field filtered by currently logged in user. The select field in this case contains a list of categories. I want to display only the categories which "belong" to the currently logged in user. The category field is a foreign key to the IngredienceCategory model.

Here is what I've come up with so far but it's giving me an error (unexpected keyword queryset). Any ideas what I'm doing wrong?

# models.py
class IngredienceCategory(models.Model):
    name = models.CharField(max_length=30)
    user = models.ForeignKey(User, null=True, blank=True)

    class Meta:
    verbose_name_plural = "Ingredience Categories"

    def __unicode__(self):
        return self.name


class Ingredience(models.Model):
    name = models.CharField(max_length=30)
    user = models.ForeignKey(User, null=True, blank=True)
    category = models.ForeignKey(IngredienceCategory, null=True, blank=True)

    class Meta:
         verbose_name_plural = "Ingredients"

    def __unicode__(self):
        return self.name


class IngredienceForm(ModelForm):
    class Meta:
        model = Ingredience
        fields = ('name', 'category')


# views.py
def home(request):
    if request.user.is_authenticated():
        username = request.user.username
        email = request.user.email
        foods = Food.objects.filter(user=request.user).order_by('name')
        ingredients = Ingredience.objects.filter(user=request.user).order_by('name')
        ingrcat = IngredienceCategory.objects.filter(user=request.user)

        if request.method == 'POST':
            form = IngredienceForm(request.POST)
            if form.is_valid():
                # Create an instance of Ingredience without saving to the database
                ingredience = form.save(commit=False)
                ingredience.user = request.user
                ingredience.save()
        else:
            # How to display form with 'category' select list filtered by current user?
            form = IngredienceForm(queryset=IngredienceCategory.objects.filter(user=request.user))

        context = {}
        for i in ingredients:
            context[i.category.name.lower()] = context.get(i.category.name.lower(), []) + [i]

        context2 = {'username': username, 'email': email, 'foods': foods, 'ingrcat': ingrcat, 'form': form,}
        context = dict(context.items() + context2.items())
    else:
        context = {}

    return render_to_response('home.html', context, context_instance=RequestContext(request))

Upvotes: 0

Views: 6348

Answers (2)

Harun ERGUL
Harun ERGUL

Reputation: 5942

Here i have another suggestion to solve the problem. You can pass request object in your form object inside view.

In view.py just pass the request object.

form = IngredienceForm(request)

In your forms.py __init__ function also add request object

from models import  IngredienceCategory as IC

class IngredienceForm(ModelForm):
    class Meta:
        model = Ingredience
        fields = ('name', 'category')

    def __init__(self, request, *args, **kwargs):
        super(IngredienceForm, self).__init__(*args, **kwargs)
        self.fields['name'].queryset = IC.objects.filter(user=request.user)

This filter always will be applied whenever you initialize your form .

Upvotes: 2

dieb
dieb

Reputation: 339

That's happening because ModelForm does not take a queryset keyword.

You can probably achieve this by setting the queryset on the view:

form = IngredienceForm()
form.fields["category"].queryset = 
        IngredienceCategory.objects.filter(user=request.user)

See related question here.

Upvotes: 4

Related Questions