encode/django-vanilla-views

Filtering Foreign Key dropdowns?

bcoover opened this issue · 2 comments

Is there a way to filter the data shown in dropdown lists in forms? I've been trying to use get_form working like this (link), but it does not seem to work with Vanilla, am I missing something?

Basically what I am trying to do is similar to that example. I need to filter the list of options from the foreign key table to only a few. In my case, I want to limit the customer names displayed to only customers that the user is allowed to see.

I think I figured it out, had to put into the super class:

def get_form(self, data=None, files=None, **kwargs):
        form = super().get_form(**kwargs)
        form.fields['company'].queryset = Company.objects.filter(id=1)
        return form

I think I figured it out

Great!