Common Fields
AutoField (primary_key = True) the primary key field as CharField (MAX_LENGTH = 32) VARCHAR (32) IntegerField () int BigIntergerField () BIGINT DecimalField () decimal EmailField () VARCHART (254) the DateField () DATE DateTimeField () datetime auto_now: per edit when data is automatically update the field time auto_now_add: automatically updated when the data is created BooleanField (field) into the field will pass Boolean value corresponds to a digital 0/1 is_delete is_status is_vip the TextField (field,) - text type store large pieces of text the FileField ( field,) - string stored in the path database, file uploading to the specified directory, file path only keep upload_to = 'specify a file path' to the field transfer file object file is automatically saved to the specified folder upload_to then save the file field route of
How to customize the char type field
Import django.db.models Field, from class RealCharField (Field,): DEF the __init __ (Self, MAX_LENGTH, args *, ** kwargs): self.max_length MAX_LENGTH # = intercept a parent class using a method of operation after completing the super call the parent class method Super () .__ the init __ (MAX_LENGTH = MAX_LENGTH, args *, ** kwargs) DEF db_type (Self, Connection): return 'char (% S)'% self.max_length
parameter choices
Enumeration is similar to:
define a large range tuple gender_choice representative may choose large number of small sets of tuple tuple small tuple put two elements, a first value is stored in the database, the second It is to demonstrate the value of the front end.
Definition field (Gender) time, which can be selected by the representative choices of the field values,
如gender = models.CharField(max_length = 32, choices = gender_choice)。
When the value of the object .get_xxx_display () such as: user_obj.get_gender_display ()
choices parameters of the user's gender education Marital status job status customers source when you list your data can be completely you can consider using the parameter choices class Userinfo (models.Model): username = models.CharField (max_length = 32) gender_choices = ( ( 1, 'M'), (2, 'F'), (3, 'other') ) Gender = models.IntegerField (choices = gender_choices) # number, or store the field and may be a number other than the matching relation record_choices = (( 'the checked', "checked"), ( "vacate", "leave"), ( "lATE", "late"), ( 'NOSHOW', "absence"), ( 'leave_early', "早退"), ) record = models.CharField ( "class record", = record_choices choices, default = "the checked", MAX_LENGTH = 64) USER_OBJ = models.Userinfo.objects.get (= PK. 1) Print (user_obj.username) Print (user_obj.gender ) # for choices parameter field value when get_xxx_display () Print (user_obj.get_gender_display ()) # acquired for data get_xxx_display not comment information () or the number itself user_obj = models.Userinfo.objects.get (pk = 4 ) Print (user_obj.gender) Print (user_obj.get_gender_display ())
Database query optimization
defer extension, extension to take in brackets
only only, just take the brackets
select_related与prefetch_related
select_related (even operating table)
select_related will be even table operation, connect all the tables have relationships, and query only once. All fields to obtain information related to the result table encapsulated object. After only need to check the properties required by a period character on the line, no database query.
the select_related () parentheses could send foreign key fields, and not many to many foreign key .
select_related (1__ foreign key foreign key foreign key 2__ 3), connected by a foreign key table indefinitely.
prefetch_related (subquery) prefetch data prefetching
By way of internal subqueries (because the database statement performs two (multiple) times, as a result of the first second conditional statement), the outer key and the associated tables all fields in this table to keep the object results , the value only need to value through a period character.
Foreign keys plurality of pairs only through this way .
https://www.cnblogs.com/tuifeideyouran/p/4232028.html
django orm open transaction operations
Four characteristics Affairs acid
Atomicity (Atomicity), consistency (Consistency), isolation (Isolation), persistent (Durability Rev)
Open database transactions
the try: SQL statement block except exception: ROLLBACK; the else: the commit;
Three database design paradigm:
First paradigm (NF1): Each field must be atomic, no longer split second paradigm (NF2): each of the primary key fields have correlation. Third paradigm (NF3): Each field is directly related to the primary key, rather than indirectly related
django orm in turn transactional operations
Import Transaction django.db from with transaction.atomic (): # ORM statements executed with the same code block belonging to a transaction pass
Other matters related configuration needs to be supplemented
MTV and MVC model
MTV
django known as MTV frame
M: Models
T: Templates
V: views
MVC
M: Models
V: views
C: Controller Controller (distributed routing the urls.py)
Essence: MTV also essentially MVC
thing