Django modelform NOT required field

后端 未结 7 1129
一整个雨季
一整个雨季 2021-01-30 01:54

I have a form like this:

class My_Form(ModelForm):
    class Meta:
        model = My_Class
        fields = (\'first_name\', \'last_name\' , \'address\')
         


        
7条回答
  •  走了就别回头了
    2021-01-30 02:36

    Guess your model is like this:

    class My_Class(models.Model):
    
        address = models.CharField()
    

    Your form for Django version < 1.8:

    class My_Form(ModelForm):
    
        address = forms.CharField(required=False)
    
        class Meta:
            model = My_Class
            fields = ('first_name', 'last_name' , 'address')
    

    Your form for Django version > 1.8:

    class My_Form(ModelForm):
    
        address = forms.CharField(blank=True)
    
        class Meta:
            model = My_Class
            fields = ('first_name', 'last_name' , 'address')
    

提交回复
热议问题