Django ORM type commonly used in the field and parameters

A, numeric

  • AutoField corresponds int (11). Increment primary keys, Django Model provided by default, it can be rewritten.
  • BooleanField corresponds tinyint (1). Boolean type field, generally used for recording status flag.
  • DecimalField the corresponding decimal. Development of accurate data demanding large businesses consider when. For example: cash = models.DecimalField (max_length, decimal_places = 2, default = 0, verbose_name = " consumption amount"), is to define a length of 8 bits, 2 bits of precision numbers, for example numbers: 666,666.66.
  • IntergerField corresponds ** int (11) **. AutoField with the same, the only difference is that no increment.
  • PositiveIntegerField . With IntegerField, it contains only positive integer.
  • SmallIntegerField correspond smallint. Generally used when small integers.

Second, the character django mapped to Mysql there are two types: longtext and varchar.

In addition TextField is longtest type, other type varchar belongs.

  • CharField correspond varchar. Varchar type basis.
  • URLField . Inherited from CharField, but to achieve a special URL for special treatment. URL used to store data, the data can reject non-URL out in the business layer, not stored in the database.
  • UUIDField corresponds char (32). In addition to use in PostgreSQL outer uuid type, in other databases are fixed length char (32), used to store the unique id generated.
  • EmailField . As with URLfield inherited from CharField, more special treatment for the email.
  • FileField . With URLField, as it inherited from CharField, for special handling of files.
  • TextField correspond longtext. Generally used for storing large amounts of text content, such as news text, blog text.
  • ImageField . Inherited from FileField, used to process images related data, on display will be different.

Third, the date type django date has three types, corresponding to the Mysql date, datetime, and time

  • DateField the corresponding date
  • DateTimeField对应datetime
  • TimeField the corresponding time

Fourth, the relationship type

  • ForeignKey , foreign key
  • OneToOneField , one on one
  • ManyToManyField , many to many

Fifth, the parameter
field is provided above django classes.

Models.CharField such as the definition is: class CharField :.

Each field which provides parameters can be selected:

  • null . It can be compared with the blank. null is used to set the database level whether to allow empty
  • blank . For the operational level, the value is allowed to be empty.
  • choices . After configuration choices fields in the admin page, you can see the corresponding display option.
  • the db_column . By default, the field is defined in the corresponding database field names can be specified Model in a field by this parameter corresponds to which field in the database.
  • db_index . Index configuration database.
  • default . The default value of the configuration.
  • editable . Whether editable, default is True. If you do not want this field displayed on the page, you can be configured as False.
  • error_messages . When prompted by an abnormal value from a defined field check fails, it is a dictionary format. value for key options as null, blank, invalid, invalid_choice, unique and unique_for_date.
  • help_text . Field prompt, after this one configuration, the corresponding field in the bottom of the page will show this configuration.
  • primary_key . A primary key, a Model allows only one field primary_key.
  • UNIQUE . The only constraint, when the need to configure unique value, set unique = True, after setting this need not be provided db_index.
  • unique_for_date . Constraints for the date (the date) in. For example, one day only to write a blog post, namely: unique_for_date = "Bowen"
  • unique_for_month。针对月份的联合约束 。
  • unique_for_month。针对年份的联合约束。
  • verbose_name。字段对应的展示文案。
  • validators。自定义校验逻辑,同form类似。

关于Django字段类型中 blank和null的区别

blank

设置为True时,字段可以为空。设置为False时,字段是必须填写的。字符型字段CharField和TextField是用空字符串来存储空值的。

如果为True,字段允许为空,默认不允许。

null

设置为True时,django用Null来存储空值。日期型、时间型和数字型字段不接受空字符串。所以设置IntegerField,DateTimeField型字段可以为空时,需要将blank,null均设为True。

如果为True,空值将会被存储为NULL,默认为False。

如果想设置BooleanField为空时可以选用NullBooleanField型字段。

一句话概括

  • null 是针对数据库而言,如果 null=True, 表示数据库的该字段可以为空。
  • blank 是针对表单的,如果 blank=True,表示你的表单填写该字段的时候可以不填,比如 admin 界面下增加 model 一条记录的时候。直观的看到就是该字段不是粗体
  • 通俗点说,该字段null=true后,你进行插入,修改操作时可以为空,然后Django把空值转换成null存在数据库中,而blank只是在表单验证的时候会检测你是否可以为空

Guess you like

Origin www.cnblogs.com/zmdComeOn/p/12157139.html