This article introduces a simple understanding of Django ORM commonly used field types and parameter configurations, the text through the sample code is very detailed, for everyone's learning or work has a certain reference learning value, you can refer to the following friends
I. Numerical
- AutoField corresponds to int(11). Self-augmenting primary keys, provided by Django Model by default, can be overridden.
- BooleanField corresponds to tinyint(1). Boolean type field, typically used to record status tags.
- DecimalField corresponds to decimal. the development of data accuracy requirements of higher business consider using. For example: cash = (max_length, decimal_places = 2, default = 0, verbose_name = "consumption amount"), is the definition of the length of 8-bit, 2-bit precision figures, such as the number: 666666.66.
- IntergerField corresponds to **int(11) **. Same as AutoField, the only difference is that it is not self-incrementing.
- PositiveIntegerField. same as IntegerField, contains only positive integers.
- SmallIntegerField corresponds to smallint, which is generally used for small integers.
II. Character type
There are two types in django corresponding to Mysql: longtext and varchar.
Except for TextField, which is of type longtest, the others are of type varchar.
- CharField corresponds to varchar. the base varchar type.
- URLField. Inherited from CharField, but implements special handling of URLs. Used to store URL data, non-URL data can be rejected in the business layer, will not be stored in the database.
- UUIDField corresponds to char(32). Except for PostgreSQL, which uses the uuid type, in all other databases it is a fixed-length char(32), which is used to hold the generated unique id.
- EmailField is inherited from CharField as URLfield, but with special handling of email.
- FileField. Like URLField, it inherits from CharField and has special handling for files.
- TextField corresponds to longtext. generally used to store a large amount of text content, such as the body of the news, the body of the blog.
- ImageField. Inherited from FileField, it is used to handle the data related to images, and will be different in the presentation.
III. Date types
There are three date types in django, corresponding to Mysql's date, datetime and time.
- DateField corresponds to date
- DateTimeField corresponds to datetime.
- TimeField corresponds to time
IV. Types of relationships
- ForeignKey, Foreign Key
- OneToOneField, one-to-one
- ManyToManyField, many-to-many
V. Parameters
The fields provided by django above are all classes.
The definition for example is: class CharField:.
Parameters are provided inside each field to choose from:
- null. can be compared to blank. null is used to set whether null is allowed at the database level.
- Blank. for the business level. whether the value is allowed to be empty.
- After you configure the choices for a field, you can see the corresponding available choices displayed on the admin page.
- db_column. by default. the defined field is the name of the corresponding field in the database. you can specify which field in the Model corresponds to which field in the database by using this parameter.
- db_index. database index configuration.
- default. the default value is configured.
- editable. editable or not, default is True. if you don't want this field to be displayed on the page, you can configure it to be False.
- error_messages. used to customize the exception prompts when a field value validation fails. which is in dictionary format. the key's value options are null, blank, invalid, invalid_choice, unique, and unique_for_date.
- help_text. field prompt, when this item is configured, the configuration will be displayed below the corresponding field on the page.
- primary_key. primary_key. only one field is allowed to be set as primary_key for a Model.
- unique. unique constraint, when need to configure the unique value, set unique=True, set this item, do not need to set the db_index.
- unique_for_date. A union constraint for dates. For example, only one blog post can be written in a day, i.e.: unique_for_date="blog post"
- unique_for_month. joint_constraint_for_month .
- unique_for_month. year-specific union constraints.
- verbose_name. field corresponding to the display text.
- Validators. custom validation logic, similar to form.
About the difference between blank and null in Django field types
blank
When set to True, the field can be empty. When set to False, the field is mandatory. Character fields CharField and TextField are used with empty strings to store null values.
If True, the field is allowed to be empty, the default is not.
null
When set to True, django uses Null to store null values. DateTimeField, DateTimeField and NumericField do not accept empty string. So when setting IntegerField, DateTimeField type fields can be null, you need to set both blank, null to True.
If True, the null value will be stored as NULL, the default is False.
If you want to set the BooleanField to be empty, you can choose the NullBooleanField type field.
summarize in one sentence
- null is for database, if null=True, it means the field can be null for database.
- blank is for the form, if blank=True, it means that you can not fill in the field when you fill in the form, for example, when you add a record of model under the admin interface. The intuition is that the field is not bold.
- In layman's terms, the field null=true, you insert, modify the operation can be null, and then Django to null value into null in the database, and blank just in the form validation will detect whether you can be empty!
This is the whole content of this article.