[Answered ]-Restrict django form ForeignKey dropdown by user

2👍

You would have to override the form field inside the init method. You could pass the logged in user to the form from the view and filter based on it

#form
class ListItemform(forms.ModelForm):
    def __init__(self, *args, ** kwargs):
            self.user = kwargs.pop('user', None)
            super(ListItemform, self).__init__(*args, **kwargs)
            self.fields['group'].queryset = Group.objects.filter(user = self.user)
#view
def displayform(request):
    user = request.user
    form = ListItemForm(user = user)
    return ...

Leave a comment