2016-09-23 40 views
0

我修改了我的表单以显示模型中字段的新标签。现在我不能让用户将表单字段留空。我试图添加空白=真和空=真,但没有运气。我怎样才能让字段为空?Django 1.9不能离开formfield空白问题

我的模型:

class UserProfile(models.Model): 
    # Page 1 
    user = models.OneToOneField(User) 
    first_name = models.CharField(max_length=100, blank=True) 
    last_name = models.CharField(max_length=100, blank=True) 
    email = models.EmailField(max_length=100, blank=True, unique=True) 

    # Page 2 Address 
    line1 = models.CharField(max_length=255, blank=True, null=True) 
    line2 = models.CharField(max_length=255, blank=True, null=True) 
    line3 = models.CharField(max_length=255, blank=True, null=True) 
    city = models.CharField(max_length=255, blank=True, null=True) 
    state = models.CharField(max_length=2, blank=True, null=True) 
    zip_code = models.CharField(max_length=15, blank=True, null=True) 

    def __str__(self): 
     return self.user.username 

forms.py:

class UserProfileForm3(forms.ModelForm): 
    line1 = forms.CharField(label="Address") 
    line2 = forms.CharField(label="") 
    line3 = forms.CharField(label="") 
    city = forms.CharField(label="City/Town") 
    state = forms.CharField(label="State/Province/Region") 
    zip_code = forms.IntegerField(label="Zip/Postal Code") 

    def __init__(self, *args, **kwargs): 
     super(UserProfileForm3, self).__init__(*args, **kwargs) 
     self.fields['line1'].widget.attrs = { 
      'class': 'form-control', 
      'placeholder': 'Address Line 1' 
     } 
     self.fields['line2'].widget.attrs = { 
      'class': 'form-control', 
      'placeholder': 'Address Line 2' 
     } 
     self.fields['line3'].widget.attrs= { 
      'class': 'form-control', 
      'placeholder': 'Address Line 3' 
     } 
     self.fields['city'].widget.attrs = { 
      'class': 'form-control', 
      'placeholder': 'City' 
     } 
     self.fields['state'].widget.attrs = { 
      'id': 'state_id', 
      'class': 'form-control', 
      'placeholder': 'State' 
     } 
     self.fields['zip_code'].widget.attrs = { 
      'id': 'zip_code_id', 
      'class': 'form-control', 
      'placeholder': 'Zip' 
     } 
     self.fields['country'].widget.attrs = { 
      'class': 'form-control', 
      'placeholder': 'US', 
      'value': 'US' 
     } 

    class Meta: 
     model = UserProfile 
     fields = ('line1', 'line2', 'line3', 'city', 'state', 'zip_code','country') 

回答

1

尝试在forms.py补充一点:

self.fields['line1'].required = false 

你必须为每一个领域做到这一点。

+1

你和Daniel的工作。但是,对于你的我也可以写一个clean_data函数来编写我自己的验证错误。另一种方法,不会让我这么做。 – Lefty

2

你推翻的领域,所以他们不会保留任何属性;你需要明确地设置它们。

line1 = forms.CharField(label="Address", required=False) 

还要注意模型的字段定义不应该有null=True为CharFields,这是不好的做法。

+0

谢谢Daniel,对于分享null = True是不好的做法。我将检查编写CharFields并纠正我的模型的正确做法。我没有意识到这是不该做的事情!说实话,我对Django很新,即使阅读文档也让我感到困惑。 – Lefty

+0

请参阅[文档](https://docs.djangoproject.com/en/1.10/ref/models/fields/#null):“避免在基于字符串的字段(如CharField和TextField)上使用null,因为空字符串值将会总是以空字符串存储,而不是NULL。如果一个基于字符串的字段为null = True,这意味着它有两个可能的值:NULL和空字符串。在大多数情况下,它是多余的两个可能的值为“无数据”; Django约定是使用空字符串,而不是NULL。“ –