Django Forms, set an initial value to request.user

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

    If you do this in your view.py instead:

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

    Then you won't trigger the validation.

    See http://docs.djangoproject.com/en/dev/ref/forms/api/#dynamic-initial-values

提交回复
热议问题