设置字段`null = True`,为什么在ModelForm中我仍然需要出错?

时间:2017-08-25 09:38:20

标签: python django

当我向我请求API时出现required错误。

在我的models.py

class User(models.Model):
    username = models.CharField(max_length=16)
    password = models.CharField(max_length=16)
    real_name = models.CharField(max_length=12, null=True)
    phone = models.CharField( max_length=11)
    email = models.EmailField(null=True)
    qq = models.CharField(max_length=10, null=True)
    address = models.CharField(max_length=64, null=True)   
    id_card = models.CharField(null=True, max_length=18, validators=[RegexValidator(regex='^.{18}$', message='id card length:18', code='nomatch')])
    id_card_img_front = models.CharField(max_length=256, null=True)
    id_card_img_back = models.CharField(max_length=256, null=True)
    nickname = models.CharField(max_length=16, null=True)
    profile = models.CharField(max_length=256, null=True, default='my profile')  
    usertype = models.ForeignKey(to='UserType', default=1)   
    ctime = models.DateTimeField(auto_now_add=True)
    uptime = models.DateTimeField(auto_now=True)    

在我的views.py中,请注意我只向请求发送usernamepasswordphone

class UserMF(ModelForm):

    class Meta:
        model = models.User
        fields = '__all__'

def register(request):
    ...
    obj = UserMF(request.POST)

    if obj.is_valid():
        ...
    else:
        print (obj.errors.as_json())  # then printed the errors
        ...

错误消息:

{"qq": [{"message": "This field is required.", "code": "required"}], "profile": [{"message": "This field is required.", "code": "required"}],   "id_card_img_front": [{"message": "This field is required.", "code": "required"}], "id_card": [{"message": "This field is required.", "code": "required"}], "real_name": [{"message": "This field is required.", "code": "required"}], "usertype": [{"message": "This field is required.", "code": "required"}], "id_card_img_back": [{"message": "This field is required.", "code": "required"}], "address": [{"message": "This field is required.", "code": "required"}], "nickname": [{"message": "This field is required.", "code": "required"}], "email": [{"message": "This field is required.", "code": "required"}]}

在我的模型中,我已将字段(用户名,密码,电话除外)设置为null=True,为什么在ModelForm中我仍然需要出错?

4 个答案:

答案 0 :(得分:1)

您可以使用"字段"在模型表单的元类中,您可以指定所需的所有字段作为

之类的集合

fields =('用户名','密码','手机')

或者您可以使用"排除"模型表单元类中的选项可以删除不需要的字段,例如exclude(" excluded_field1"," excluded_field2")等等

您可以在modelform

找到相关说明

答案 1 :(得分:1)

null=True上同时使用blank=TrueCharField,您将删除此必需的检查,但删除null=True会更好。 Django's advice

  

避免在基于字符串的字段(例如CharFieldTextField)上使用null。如果基于字符串的字段具有null=True,则表示它具有“无数据”的两个可能值:NULL和空字符串。

由于您似乎仅将CharFieldnull=True一起使用,我会将所有这些内容更改为blank=True

答案 2 :(得分:0)

请尝试将blank=True添加到所有可选字段中。

您可以在此处找到有关blank参数的更多信息:https://docs.djangoproject.com/en/1.11/ref/models/fields/#blank

答案 3 :(得分:0)

null=True仅表示在数据库级别允许该字段为NULL。表单(和模型)验证不会受此选项的直接影响,您需要的是blank=True选项。

另外和FWIW一样,为charfields / textfields允许NULL通常是一个非常糟糕的主意,因为你有两个不同的“空”值(NULL和空字符串),所以对于你想要的可选字符/文本字段{{1}只使用空字符串作为空值(nb:仍然有用于同时具有NULL和空字符串的用例,但从大多数时候经验只有空字符串使生活更容易)。

最后,您还可以限制blank=True, null=False, default=''中使用的字段集(通过使用ModelForm属性明确列出您想要的字段 - 这是推荐的方式 - 或者通过排除某些字段fields属性)。