Django管理可空字段的错误报告错误

时间:2015-07-25 08:29:40

标签: django

这是我的用户类:

class User(TimeStampedModel, AbstractBaseUser):                                             
    name = models.CharField(null=False, max_length=255)                                     
    username = models.CharField(null=False, unique=True, max_length=255)                    
    email = models.EmailField(null=True)                                       
    mobile_number = models.CharField(null=True, max_length=255)                             
    bio = models.CharField(null=True, max_length=255)                                       
    title = models.CharField(null=True, max_length=255)                                     
    posts = models.IntegerField(default=0)                                                  
    views = models.IntegerField(default=0)                                                  
    is_active = models.BooleanField(default=True)                                           
    is_staff = models.BooleanField(default=False)                                           
    last_active_on = models.DateTimeField(auto_now=True)                                    


    objects =  CustomUserManager()                                                          

    USERNAME_FIELD = 'username'                                                             
    REQUIRED_FIELDS = ['name']     

当我尝试从管理信息中心更新用户时,我会在以下字段中显示“字段不能为空错误”:mobile_number, email, bio, title

有关如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:2)

在模型中为这些字段指定blank=True

class User(TimeStampedModel, AbstractBaseUser):                                             
    name = models.CharField(null=False, max_length=255)                                     
    username = models.CharField(null=False, unique=True, max_length=255)                    
    email = models.EmailField(null=True, blank=True)  # add 'blank' argument                                     
    mobile_number = models.CharField(null=True, max_length=255, blank=True) # add 'blank' argument                                                          
    bio = models.CharField(null=True, max_length=255, blank=True) # add 'blank' argument                                                                     
    title = models.CharField(null=True, max_length=255, blank=True) # add 'blank' argument                                                                   
    posts = models.IntegerField(default=0)                                                  
    views = models.IntegerField(default=0)                                                  
    is_active = models.BooleanField(default=True)                                           
    is_staff = models.BooleanField(default=False)                                           
    last_active_on = models.DateTimeField(auto_now=True)                                    


    objects =  CustomUserManager()                                                          

    USERNAME_FIELD = 'username'                                                             
    REQUIRED_FIELDS = ['name']   

如果某个字段有blank=True,则表单验证将允许输入空值。 blank参数的默认值为False

Note: null纯粹是数据库相关,而blank 验证相关

如果某个字段有blank=True,则表单验证将允许输入空值。

如果字段有null=True,Django会将空值存储为数据库中的NULL