32

I would like to make a queryset where the current user is used as a filter in a ModelForm:

class BookSubmitForm(ModelForm):
    book = forms.ModelChoiceField(queryset=Book.objects.filter(owner=request.user),)
...

Does Django pass the request to the form? Is it good practice? How can I use the request? (of course the name request is not defined)

Edit:

I tried another solution which is to call the form in the view passing it the request:

form = BookSubmitForm(request)

and then in the form I use this:

class BookSubmitForm(ModelForm):
    def __init__(self, request, *args, **kwargs):
        super(BookSubmitForm, self).__init__(*args, **kwargs)
        self.fields["library"].queryset = Library.objects.filter(owner=request.user)

It works and the code is in the form. Now I'm not sure it's the best solution, could it be improved?

Bastian
  • 5,625
  • 10
  • 44
  • 68
  • Django docs: [Models and request.user](https://docs.djangoproject.com/en/2.1/topics/class-based-views/generic-editing/#models-and-request-user) – Brad Solomon Sep 20 '19 at 15:12

2 Answers2

46

No, the request is not passed to the ModelForm. You'll need to do something like this in your view:

form = BookSubmitForm()
form.fields['book'].queryset = Book.objects.filter(owner=request.user)
# pass form to template, etc

As you said, it's often cleaner to encapsulate this in the Form object, particularly if you have several fields that will need filtered querysets. To do this, override the forms's __init__() and have it accept a kwarg of request:

class BookSubmitForm(ModelForm):
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop("request")
        super(BookSubmitForm, self).__init__(*args, **kwargs)
        self.fields["book"].queryset = Book.objects.filter(owner=self.request.user)
        self.fields["whatever"].queryset = WhateverModel.objects.filter(user=self.request.user)

Then just pass request whenever you instantiate BookSubmitForm in your view:

def book_submit(request):
    if request.method == "POST":
        form = BookSubmitForm(request.POST, request=request)
        # do whatever
    else:
        form = BookSubmitForm(request=request)
    # render form, etc
AdamKG
  • 13,678
  • 3
  • 38
  • 46
  • 1
    You're right, it works. I would like to know if it is possible to put the code in the form though, just for organizational purpose and global cleanliness. – Bastian Jan 13 '12 at 07:02
  • 1
    Yes, it's possible, and it's often a good idea in order to keep your view code clean. I've updated with an example. – AdamKG Jan 13 '12 at 11:33
13

Extending AdamKG answer to class based views - override the get_form_kwargs method:

class PassRequestToFormViewMixin:
    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs['request'] = self.request
        return kwargs

from django.views.generic.edit import CreateView
class BookSubmitCreateView(PassRequestToFormViewMixin, CreateView):
    form_class = BookSubmitForm
# same for EditView

and then in forms:

from django.forms import ModelForm
class BookSubmitForm(ModelForm):
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop("request")
        super().__init__(*args, **kwargs)
        ...
Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
trybik
  • 482
  • 6
  • 16