Today, when doing database design, the following user table was designed, where I set the email and phone fields to allow null:
1 classUser (models. Model):2Username = models. Charfield ('User name', max_length=50)3Password = models. Charfield ('Password', max_length=255)4email = models. Emailfield ('Email',null= True)5Phone = models. Charfield ('Telephone', max_length=11,null= True)6 7Group = models. Manytomanyfield (usergroup,verbose_name='User Groups')8 9Create_date = models. Datetimefield ('creation Time', auto_now_add=True)TenUpdate_date = models. Datetimefield ('Last Modified Time', auto_now=true)
But when I add data using Django background management, I find that I can't enter an empty field, and I still need to enter the content.
When you use Django to design a database table, the data is not known
If you set null=true, it only means that the field in the database can be empty, but you still need to enter a value when adding data using background management, because Django automatically does the data validation does not allow the field to be empty
If you want to save a field as a null value in Django, you need to add another parameter: blank=true
In general, it is:
Null=True if the field in the database can be emptyblank =True When adding data to Django Admin allows null values
Therefore, to implement adding null values in Django background management, you should design the table as follows:
1 classUser (models. Model):2Username = models. Charfield ('User name', max_length=50)3Password = models. Charfield ('Password', max_length=255)4email = models. Emailfield ('Email',null=true,blank= True)5Phone = models. Charfield ('Telephone', max_length=11,null=true,blank= True)6 7Group = models. Manytomanyfield (usergroup,verbose_name='User Groups')8 9Create_date = models. Datetimefield ('creation Time', auto_now_add=True)TenUpdate_date = models. Datetimefield ('Last Modified Time', auto_now=true)
How fields are empty in the Django database design