Python Django model layer

A single-table operation

= create_time models.DateField () # do not specify auto_now or auto_now_add , you need to pass their own parameters 
critical parameters:
1 .auto_now: operating data will automatically refresh every time the current operation
2.auto_now_add: when data is created automatically the creation time record subsequent changes will not affect the field

Two test script files

django you can write a script for a single test to test a py files do not need to take frequent web requests

1. script file must test code

Import os
 Import SYS 

IF  __name__ == " __main__ " :
     # Django will set a key to global large dictionary when you start the path string value is exposed to the user's profile 
    os.environ.setdefault ( " the DJANGO_SETTINGS_MODULE " , " day54.settings " ) 

    Import Django 
    django.setup () 
    
# the following test code: app01 applications such as testing and it Book class files models.py
    from app01 import models 
models.Book.objects.all()
   

Single table operating the three-layer model

1 . Returned QuerySet object methods 
All () 

filter () 

the exclude () 

ORDER_BY () 

Reverse () 

DISTINCT ()

 2 . Special QuerySet 
values () Returns dictionary sequence an iterative 

values_list () Returns an iterative ancestral sequence

 3 . returns particular object 
GET () 

First () 

Last ()

 . 4 . the method has the Boolean value: 
EXISTS ()

 . 5 . returns the methods 
count ()
View Code

Specific operation (see the connector and complementary)

https://www.cnblogs.com/Dominic-Ji/p/9203990.html

1. order_by (* field): order query results ( ' -id ' ) / ( ' . Price ' )
 Print (models.Book.objects.order_by ( ' . Price ' ))   # default ascending 
Print (models.Book.objects .order_by ( ' -Price ' ))   # minus sign is descending
 
2. reverse (): reverse sort query results >>> front first have to reverse the sort
 # Print (models.Book.objects.order_by ( ' .. price ') Reverse ())
 
3 . DISTINCT (): from the returned results eliminate duplicate records
     . "" " 
    go heavy on the premise that it must be a query that the results have a complete duplication of data in order to go heavy 
    ." "" 
 # Print (Models .Book.objects.filter (title = 'Three Kingdoms') .distinct ())

 

Guess you like

Origin www.cnblogs.com/tfzz/p/11546386.html