This is my views.py
def new_note(request):
form = NoteForm(request.POST, user=request.user)
. . .
return render(request, 'newNote.html', {'note_form': NoteForm, 'registered': registered})
This is my forms.py
class NoteForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
# self.user = kwargs.pop('user', None)
self.user = kwargs.pop('user')
# self.validate = kwargs.pop('validate', False)
super(NoteForm, self).__init__(*args, **kwargs)
groups = StudentGroup.objects.filter(id_nauczyciela=self.user.pk)
# print(self.user.pk)
self.fields['id_groups'].queryset = groups
note_name = forms.CharField(label="Note name", required=True)
class Meta:
model = Note
fields = ('id_groups', 'id_students', 'note_name')
id_groups is ModelChoseField
In this code i try take logged user id.
When i try use it i got error KeyError 'user', when i change self.user = kwargs.pop('user') to self.user = kwargs.pop('user', None) i got error like this
AttributeError: 'NoneType' object has no attribute 'pk'
I am absolutely sure self.user.pk return value what i expected.
My question is How deal with this type of error?