1. Define Properties
Django determine the following information according to the type 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 creates a primary key column for the table automatically increase each model can have only one primary key column, django will not create a primary key column after the automatic growth of key columns if you use option to set a property-based.
The primary key column is created default properties for the id, it can be used instead of pk, pk spelling for the primary key.
pk is the primary key of the alias, if the primary key named id2, id2 then pk is an alias.
Property naming restrictions:
- Python can not be reserved keywords.
- Do not allow the use of a continuous underscore, which is determined by the django query, in Section 4 will be explained in detail inquiry.
- You need to specify the type of field when defining attribute that specifies the type of parameter options through the field.
Already used the simple, but it did not specify the syntax:
属性=models.字段类型(选项)
1.1 Field Type
(1) AutoField
Automatic growth IntegerField, usually not specified, Django will automatically create a property called id attribute grow automatically when not specified.
Customizing a primary key:
my_id=models.AutoField(primary_key=True)
(2) BooleanField
Boolean field, a value of True or False.
(3) NullBooleanField
Similarly BooleanField, support Null, True, False three values.
(4) CharField (max_length = character length)
String.
Max_length parameter indicates the maximum number of characters.
(5) TextField
Large text field, generally used when more than 4000 characters.
(6) IntegerField
Integer.
(7) DecimalField(max_digits=None, decimal_places=None)
Decimal floating point.
Max_digits parameter represents the total number of digits.
Parameters decimal_places represent decimal places.
(8) FloatField
Floating-point number.
(9) DateField[auto_now=False, auto_now_add=False])
date.
Auto_now parameters indicate each time to 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.
Automatically sets the parameters auto_now_add means that when an object is first created when the current time, for the time stamp created, it always uses the current date, the default is false.
Auto_now_add and auto_now parameters are mutually exclusive, the combination of errors will occur.
(10) TimeField
Time parameters with DateField.
(11) DateTimeField
Date and time parameters are the same DateField.
(12) FileField
Upload file field.
Parameters: upload_to, upload a file to save the local file system path.
(13) ImageField
Inherited from FileField, to verify the content uploaded to ensure a valid image.
There are two optional parameters: height_field and width_field. Save the picture in accordance with the height and width specifications provided.
(14) CommaSeparatedIntegerField(max_length)
Integer fields separated by commas.
CharField the same parameters.
(15) FilePathField(path,[match],[recursive])
Fields with multiple options, the options are limited to a file system file names in a directory.
(16) EmailField
CharField check whether the value is a valid email address.
(17) IPAddressField
IP address, as a string format.
For example: "192.168.0.1"
(18) PhoneNumberField
Check whether the value is a legitimate American phone format.
(19) PositiveIntegerField
And similar IntegerField, but it must be positive.
(20) SlugField
Short content of the label, this content can only contain letters, numbers, underscores or hyphens. Typically used for URL.
(21) Small Integer Field
And IntegerField similar, but only values within a range associated database allows (usually -32,768 to +32,767)
(22) URLField
Used to store the URL field.
(23) USStateField
US state initials, two letters.
(24) XMLField(schema_path)
Similarly TextField, just to check whether the value of the XML schema matches the specified legal.
1.2 Options
Implementation constraints of the field through the options.
null: If True, representation allows empty, the default value is False.
blank: If True, this field is blank allows default is False.
要注意,这与 null 不同。null纯粹是数据库范畴的,而blank是数据验证范畴的。
如果一个字段的blank
=
True
,表单的验证将允许该字段是空值。如果字段的blank
=
False
,该字段就是必填的。
db_column: name of the field, if not specified, the name of the property use.
db_index: If the value is True, then the table will create an index for this field, the default value is False.
default: default.
primary_key: If True, the field will be the primary key field model, the default value is False, generally as an option AutoField of.
unique: If True, this field must have a unique value in the table, the default value is False.
choices: objects comprising a double iteration tuple for providing options to the field.
editable: If False, the field or in the management interface in the form will not be edited. The default is True.
help_text: Extra Help text in the field below the form in the admin interface object.
radio_admin: By default, for ForeignKey or have choices set of fields, Django admin interface will use the list to select the box (<select>). If radio_admin set to True then, Django will use radio buttons interface.
unique_for_date: its value is set to the name of a DateField or DateTimeField field can be ensured field does not appear in duplicate values for this date.
unique_for_month: and unique_for_date similar, but requires the field to be unique within a month of the specified field.
unique_for_year: and unique_for_date and unique_for_month similar, but the time turned into a year.
verbose_name: fields except ForeignKey, ManaToManyField OneToOneField and accept a detailed name of the first position as a parameter.
1.3 simple demonstration
# Book class class BookInfo (models.Model): '' ' Book model class ' '' # book title BTITLE = models.CharField (= max_length 20, the db_column = ' title ' ) # books name the only # BTITLE = models.CharField ( 20 = max_length, UNIQUE = True, db_index = True) # price, the maximum number is 10, the decimal is 2 # bprice = models.DecimalField (max_digits = 10, decimal_places = 2) # publication date bpub_date = models.DateField () # bpub_date = models.DateField (auto_now_add = True) # create a time # bpub_date = models.DateField (auto_now = True) # update # amount of reading = models.IntegerField Bread (default = 0) # Comment amount bcomment = models.IntegerField (default = 0) # remove the mark isDelete = models.BooleanField (default = False) class RoleInfo (models.Model): '' ' role model character class ' '' # Role name RNAME = models.CharField (max_length = 20 ) # gender rgender = models.BooleanField (default = False) # description of rcomment = models. as CharField (= 200 is MAX_LENGTH, null = True, blank = False) # relationship properties rbook = models.ForeignKey ( ' the BookInfo ' , on_delete = models.CASCADE) # delete flag isDelete = models.BooleanField (default = False)