How fields are empty in the Django database design

Source: Internet
Author: User

Null=true the field in the database can be NULL when you add data to the Blank=true Django Admin


Original:

Today, when doing database design, the following user table was designed, where I set the email and phone fields to allow null:

1 class User (models. Model): 2     username = models. Charfield (' username ', max_length=50) 3     password = models. Charfield (' password ', max_length=255) 4     email = models. Emailfield (' mailbox ',null=True) 5     phone = models. Charfield (' phone ', max_length=11,null=True) 6  7     group = models. Manytomanyfield (usergroup,verbose_name= ' user Group ') 8  9     create_date = models. Datetimefield (' Creation time ', auto_now_add=true)     update_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 the field in the database can be NULL when you add data to the Blank=true Django Admin
Therefore, to implement adding null values in Django background management, you should design the table as follows:
1 class User (models. Model): 2     username = models. Charfield (' username ', max_length=50) 3     password = models. Charfield (' password ', max_length=255) 4     email = models. Emailfield (' mailbox ',null=true,blank=True) 5     phone = models. Charfield (' phone ', max_length=11,null=true,blank=True) 6  7     group = models. Manytomanyfield (usergroup,verbose_name= ' user Group ') 8  9     create_date = models. Datetimefield (' Creation time ', auto_now_add=true)     update_date = models. Datetimefield (' Last Modified time ', auto_now=true)
Originating From: http://www.cnblogs.com/MacoLee/p/5610989.html

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.