UpdateIssueForm.__init__() missing 1 required keyword-only argument: 'pk'

I tried making an update view using class-based views. I have not known how to pass this kwarg to the view. I have ended up with an error. Here is the view and the form View

class UpdateTeacherIssueView(LoginRequiredMixin,UpdateView):
    model = TeacherIssue
    form_class = UpdateTeacherIssueForm
    template_name = 'crud_forms/edit_teacher_issue.html'
    success_url = reverse_lazy('view_student') #To be changed
    
    def get_form_kwargs(self):
        kwargs = super(UpdateTeacherIssueView, self).get_form_kwargs()
        kwargs['school'] = self.request.user.school
        kwargs['issuer'] = self.request.user
        #kwargs['pk'] = self.request.id
        return kwargs

The form

class UpdateTeacherIssueForm(forms.ModelForm):
    """Edit TeacherIssue Form"""
    def __init__(self,*args, pk, school,issuer, **kwargs):
        super(TeacherIssueForm, self).__init__(*args, **kwargs)
        self.fields['issuer'].initial = issuer
        self.fields['book_id'].queryset = Books.objects.filter(school=school,no_of_books=1).select_related('subject')
        self.fields['borrower_teacher'].initial = pk
    class Meta:
        model = TeacherIssue
        fields = ['issuer','book_id','borrower_teacher','issue_date']
        widgets = {
            'borrower_teacher':forms.TextInput(attrs={"class":'form-control','type':'hidden'}),
            "issue_date":forms.TextInput(attrs={"class":"form-control"}),
            'issuer':forms.TextInput(attrs={"class":'form-control','type':'hidden'}),
            'book_id':Select2Widget(attrs={'data-placeholder': 'Select Book','data-width': '100%'},)
        }

your init function in ModelForm is not good:

    def __init__(self, pk, school,issuer,*args, **kwargs):
        super(UpdateTeacherIssueForm, self).__init__(*args, **kwargs)
        self.fields['issuer'].initial = issuer
        self.fields['book_id'].queryset = Books.objects.filter(school=school,no_of_books=1).select_related('subject')
        self.fields['borrower_teacher'].initial = pk

*args and **kwargs have to be placed at end of arguments function. and In call of super you have to set the name of your current class.

You have to uncomment the kwargs['pk'] = self.request.id

Not sure if it is the problem: you do not show any error.

Back to Top