Django Data Model -- common field options, django Field

Source: Internet
Author: User

Django Data Model -- common field options, django Field
Field

The most important and only necessary part of a model is the database fields it defines.

Field name restrictions

1. The field name cannot be a reserved word in python, which may cause a python syntax error.

2. The field cannot contain more than one consecutive underline, which may conflict with the Django query statement syntax.

Common field options

NullIf set to True, Django stores NULL values in the database. The default value is False.

BlankIf it is True, this field can be left blank. The default value is False.

ChoicesAn iteratable object that contains dual-element tuples. It is used to provide options for fields.

Db_columnName of the column corresponding to the current field in the database

Db_indexIf this parameter is set to True, a database index is applied to this column when a table is created.

DefaultField Default Value

EditableIf it is False, this field cannot be edited on the management interface or form. The default value is True.

Help_textAdditional help text displayed under fields in the form object on the management interface. This attribute is useful even if you have not managed the form.

Primary_keyIf it is True, this field becomes the primary key of the model.

Radio_adminFor ForeignKey or fields with choices settings, the Django management interface uses the list selection box (<select> ). If radio_admin is set to True, Django uses the radio button interface. If the field is not ForeignKey or is not set with choices, do not use this option for the field.

UniqueIf it is True, the value of this field must be unique throughout the table.

Unique_for_dateSet its value to the name of a DataField or DateTimeField field, so that the field does not have repeated values within this date.

Unique_for_monthSimilar to unique_for_date, it only requires that the field be unique within the month of the specified field

Unique_for_yearSimilar to unique_for_date and unique_for_month, the time range is changed to one year.

Verbose_nameFor fields other than ManyToManyField and OneToOneField, a detailed name is used as the first location parameter. If the detailed name is not specified, Django converts the underline in the attribute name of the field into a space string as the detailed name.

For example, the detailed name is "Person's first name": first_name = models. charField ("Person's first name", maxlength = 30) in the following example, the detailed name is "first name": first_name = models. charField (maxlength = 30) ForeignKey, ManyToManyField, and OneToOneField require the first parameter to be a model class. Therefore, you can only use the keyword parameter verbose_name: poll = models. foreignKey (Poll, verbose_name = "the related poll") sites = models. manyToManyField (Site, verbose_name = "list of sites") place = model S. OneToOneField (Place, verbose_name = "related place") Conversion does not capital the first letter of verbose_name, and Django automatically capital the first letter as needed.

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.