Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering a model in a CreateView with get_queryset

I'm trying to filter a model with get_queryset() and it seems to work in the view but not in the template.

My view :

class FolderCreate(CreateView):
    fields = ['name', 'parent']
    template_name = 'Form/folder_create.html'

    def get_queryset(self):
        folders = Folder.objects.filter(owner=self.request.user) 
        print folders # ==> return [<Folder: Folder>, <Folder: Another folder>]
        return folders

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.owner = self.request.user
        return super(FolderCreate, self).form_valid(form)

    def get_initial(self):
        if self.request.method == 'GET':
            foldersUrl = self.request.META['HTTP_REFERER'].split('/')
            foldersUrl.pop()
            folder = urllib2.unquote(foldersUrl[-1])

            try:
                return {'parent' : Folder.objects.get(name=folder, owner=self.request.user)}
            except Folder.DoesNotExist:
                pass

As you can see, folders return two objects related to the session user in get_queryset() : 'Folder' and 'Another folder

Infortunately, the combobox of my template get all the folders, without any filtering.

enter image description here

Any idea ?

like image 802
Burrich Avatar asked Jun 04 '14 15:06

Burrich


1 Answers

The issue here is that get_queryset is not used in a CreateView, as it's meant for filtering the models returned for display in a list or detail view. You want something completely different: you want to filter the choices available in a form field.

To do that you will need to create a custom ModelForm that accepts a user kwarg and filters the queryset accordingly:

class FolderForm(forms.ModelForm):
    class Meta:
       model = Folder
       fields = ['name', 'parent']

    def __init__(self, *args, **kwargs):
       user = kwargs.pop('user')
       super(FolderForm, self).__init__(*args, **kwargs)
       self.fields['parent'].queryset = Folder.objects.filter(user=user)

and then change your view to use that form and pass in the user parameter:

class FolderCreate(CreateView):
    template_name = 'Form/folder_create.html'
    form_class = FolderForm

    def get_form_kwargs(self):
        kwargs = super(FolderCreate, self).get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs
like image 196
Daniel Roseman Avatar answered Oct 21 '22 10:10

Daniel Roseman