[Fixed]-Django ModelChoseField limited to current logged user

1👍

You will need to pass the current user to the from when you initialize it in the view: form = MyForm(user=request.user), then you can do something like the following in your form:

class MyForm(ModelForm):
    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        super(MyForm, self).__init__(*args, **kwargs)
        groups = StudentsGroup.objects.filter(id_teacher=self.user.pk)
        self.fields['id_group'].queryset = groups

Leave a comment