Django common fields, database query optimization only and defer

Common fields:

        AutoField () int Primary Key AUTO_INCREMENT 
        CharField () VARCHAR () 
        IntegerField () int () 
        Big ....             
        EmailField () VARCHAR ( 254 ) 
        DateField () DATE 
        DateTimeField () datetime 
            auto_now: modifying the data will be updated every time 
            auto_now_add: update only once when you first create the data 
            
        BooleanField (field,) 
            is_delete = BooleanField () 
            to pass the value of the field when you only need to pass a boolean value 
            but it corresponds to a stored database is 0 and 1 
        TextField ( Field,)
             - type of text 
            used to keep large pieces of text 
        
        the FileField (Field,)
         -String stored in the path database, file uploading to the specified directory
         - parameters: 
            the upload_to = ""          users to upload file automatically into the specified file path equal sign 
            Storage = None storage component, the default django.core.files.storage .FileSystemStorage

 

Custom char fields:

        class MyChar(models.Field):
            def __init__(self,max_length,*args,**kwargs):
                self.max_length = max_length
                super().__init__(max_length=max_length,*args,**kwargs)

            def db_type(self, connection):
                return 'char(%s)'%self.max_length

 

Foreign key field:

            When you use django2.X version in establishing a foreign key relationship (***** ) 
            requires you to manually add a few key parameters 
                models.cascade 
              db_constraints

 

Database query optimization:

        only with the defer 
            
        select_releated and prefect_releated 
        
        "" " database query optimization " "" 
            # all operations within the statements are inert orm query: database will only go when you really need the data, and if you just write the statement does not only orm the database will go 
            # benefits of this design is to reduce the pressure on the database 

            # RES = models.Book.objects.all () 
            # Print (RES) 


            # RES = models.Book.objects.values ( 'title') 
            # # Print ( RES) 
            # for R & lt RES in: 
            #      Print (r.title) 


            # RES = models.Book.objects.only ( 'title') 
            # # Print (RES) 
            # for R & lt RES in: 
            #      # Print (r.title) # only go once a database query 
            #     print (r.price) # is not only when you click on a specific field in brackets when not error but will frequently take the database query 


            # res1 = models.Book.objects.defer ( 'title') and only the defer # It is the opposite of 
            # for r in res1: All the information in the fields # defer all brackets will not check out the package object 
            #      # Once you click on the field in brackets so frequently go database queries 
            #      Print (r.price ) 



            # select_related and prefetch_related 
            # select_related help you connect directly query the data table operations within parentheses only put the foreign key field 
            # RES = models.Book.objects.all () select_related ( 'publish'). 
            # for r in RES: 
            #      Print (r.publish.name) 
            # RES = models.Book.objects.all (). select_related ( 'publish__xxx__yyy__ttt') 
            # Print (RES) 
            # RES = models.Book.objects.all ()
            "" " 
            Select_related: that table will be associated with all brackets and external key fields directly take over (you can get a one-time multiple tables) with the current table the splicing operation 
            to reduce pressure across the table you query a database of 
            
            attention select_related only put brackets foreign key field (one and one-to-many) 
             RES = models.Book.objects.all (). select_related ( 'foreign key fields 1__ foreign key fields 2__ foreign key fields 3__ foreign key field 4') 
            "" " 
            # prefetch_related not even take the initiative to table 
            RES = models.Book.objects.prefetch_related ( ' publish ' )
             " "" 
            do not even take the initiative to table operation (but inside you feel like even the operating table) but the book table publish all taken out all data in the corresponding table ID taken publish 
            res = models.Book.objects.prefetch_related ( 'publish') 
            has several foreign key field will take several database queries in parentheses     
            "" "
            for r in res:
                print(r.publish.name)
        

 

Transaction:

    Transaction 
        ACID 
            Atomicity 
            Consistency 
            Isolation 
            persistence 
        from django.db Import Transaction 
    
        with transaction.atomic (): 
            "" " database operations 
            written in the block belong to a transaction operation 
            " "" 
            models.Book.objects.create () 
            models.Publish.objects.create () 
            # add books and publishers is the same transaction either succeed together or fail together 
        Print ( ' the code block the transaction is over ' )

 

Guess you like

Origin www.cnblogs.com/xiaowangba9494/p/11573486.html