Django Forms, set an initial value to request.user

前端 未结 4 2040
攒了一身酷
攒了一身酷 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:45

    You might want to handle this in your view function. Since your view function must create the initial form, and your view function knows the user.

    form = JobRecordForm( {'supervisor':request.user} )
    

    This will trigger validation of this input, BTW, so you can't provide hint values this way.

提交回复
热议问题