Django Forms, set an initial value to request.user

前端 未结 4 2163
攒了一身酷
攒了一身酷 2021-02-01 07:51

Is there some way to make the following possible, or should it be done elsewhere?

class JobRecordForm(forms.ModelForm):
    supervisor = forms.ModelChoiceField(
         


        
4条回答
  •  忘掉有多难
    2021-02-01 08:41

    For a complete answer, here's the CBV solution:

    class MyFormView(TemplateView, FormMixin):
        def get_initial(self):
            self.initial.update({'your_field': self.request.user})
            return super(MyFormView, self).get_initial()
    

提交回复
热议问题