How to give initial value in modelform

前端 未结 2 2175
梦如初夏
梦如初夏 2021-02-12 21:44

How do I assign initial values to fields inside a ModelForm? eg:

class Form1(forms.Form):
    title=forms.CharField(initial=\"hello\")

What wil

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-12 22:08

    Model Form defaults are taken from the model itself. For example:

    class SomeModel(models.Model):
        title  = models.CharField(max_length=45, default="hello")
    

    You don't need to make any changes to your ModelForm:

    class RequestForm(forms.ModelForm):
    
        class Meta:
            model = SomeModel
    

提交回复
热议问题