Django- defined attributes

https://www.bilibili.com/video/av57516522/?p=13

Outline

  • django determine the following information according to the type of the attribute
    • The type of database to support the currently selected field
    • The default html controls use when rendering management form
    • At a minimum management site verification
  • django will increase the growth of primary key columns automatically for the table, each model can have only one primary key column, if the option to set an attribute primary key column, the default django no longer generate the primary key column

  • Property naming restrictions
    • Python can not be reserved keywords
    • Since the query django, and does not allow the use of a continuous underscore

Storehouse

  • Defining properties required field type, the field type is defined in the directory django.db.models.fields, for convenience, it is introduced into the django.db.models

  • Use
    • 导入from django.db import models
    • By creating an object field type models.Field, assigned to the property

Tombstone

  • For important data to do the tombstone, not physically deleted, implementation is defined isDelete attribute, type BooleanField, the default value is False

Field Type

  • Auto Field
    • According to a real ID IntegerField automatic increase, usually do not specify if not specified, a primary key field is automatically added to the model
  • CharField (max_length = character length)
    • String, the default style sheet is TextInput
  • TextField
    • Large text fields, more than 4,000 general use, the default form controls are Textarea
  • IntegerField
    • Integer
  • DecimalField(max_digits=None, decimal_places=None)
    • Decimal floating-point numbers using the decimal instance represents python
    • Parameter Description
      • DecimalField.max_digits
        • The total number of digits
      • DecimalField.decimal_places
        • The number of digits after the decimal point
  • FloatField
    • Float float with the example represented Python
  • BooleanField
    • true / false field, the default field of this form of control is CheckboxInput
  • NullBooleanField
    • Support null, true, false three values
  • DateField[auto_now=False, auto_now_add=False])
    • Date using Python instance represents datetime.date
    • Parameter Description
      • DateField.auto_now
        • Each time you save the object, the field is automatically set to the current time, for the "last modified" timestamp, it always uses the current date, the default is false
      • DateField.auto_now_add
        • When an object is first created automatically set to the current time, for the time stamp created, it always uses the current date, the default is false
    • Explanation
      • The field defaults to the corresponding form control is a TextInput. In the site administrator adds a calendar written in JavaScript controls, and a "Today" shortcut button, contains an additional error message key invalid_date
    • note
      • auto_now_add, auto_now, and default settings are mutually exclusive, any combination between them will be wrong results occur
  • TimeField
    • Examples of using Python datetime.time time represented by the parameter with the DateField
  • DateTimeField
    • Date and time represented by the Python datetime.datetime example, the same parameter DateField
  • FileField
    • A file upload field
  • ImageField
    • Inherits all the attributes and methods FileField, but object to upload can be verified to ensure that it is a valid image

Field Options

  • Outline
    • Through field options, can be achieved constraints on the field
    • Specified by keyword parameters in the field object
  • null
    • If True, Django will empty NULL values ​​stored in the database, the default value is False
  • White person
    • If True, the field is allowed to be blank, the default value is False
  • note
    • null is a conceptual database categories, blank form validation certificate is Category
  • db_column
    • Name of the field, if not specified, the name of the property
  • db_index
    • If the value is True, then the table will create an index for this field
  • default
    • Defaults
  • primary_key
    • If it is True, then the field will be the primary key field model
  • unique
    • If True, this field must have a unique value in the table

relationship

  • classification
    • ForeignKey: many, the number of fields defined in the ends
    • ManyToManyField: many-field defines the two ends
    • OneToOneField: one, the field definitions at either end of the
  • With a multi-access
    • format
      • Object. _Set model class in lowercase
    • Examples
      grade.students_set
  • A visit with a
    • format
      • Object The model class in lowercase
    • Examples
      • grade.students
  • Access id
    • format
      • Object. _Id attribute
    • Examples
      • student.sgrade_id

Guess you like

Origin www.cnblogs.com/yihutu/p/11951699.html