encode / django-vanilla-views

Beautifully simple class-based views.
http://django-vanilla-views.org/
BSD 2-Clause "Simplified" License
985 stars 74 forks source link

Filtering Foreign Key dropdowns? #59

Closed bcoover closed 7 years ago

bcoover commented 7 years ago

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.

bcoover commented 7 years ago

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
tomchristie commented 7 years ago

I think I figured it out

Great!