How fields are empty in the Django database design

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.