Django - ORM Advanced Search

Advanced Query
 # 1. in = [1, 2] query through field name __in 
    RES = models.Student.objects.filter (age__in = [12,14,42 ]). All () 

# 2. By not in exclude ( field name __in = [1,2]) exclude that in addition to the meaning 
    RES = models.Student.objects.exclude (age__in = [12,14,42 ]). All () 

# 3. like wildcard query 
    # WHERE name like "Lee%" # representative for all names beginning with Lee 
    # represents name__istartswith case insensitive to what is beginning 
    RES = models.Student.objects.filter (name__startswith = " Lee " ) .all () 

    # the WHERE name like "% white" # indicates matches all words ending with white 
    # name__iendswith representation is not case sensitive, to what ends with 
    RES = models.Student.objects.filter (name__endswith = "White " ) .all () 

    # WHERE name like"% Small% "means match all words middle small print 
    # name__icontains represents insensitive, comprising what words 
    RES = models.Student.objects.filter (= name__contains " small " ) .all () 

# 4. BETWEEN ... .. and column names through __range = [start position, end position] closed interval 
    RES = models.Student.objects.filter (id__range = [2,5 ]) .all () 

# 5. the limit sliced [10:20] represents an index by taking from 10 to 20 after closing the front opening 
    RES = models.Student.objects.all () [. 5:. 7 ] 

# 6. the order by order by The method by, preceded by "-" is descending, ascending default 
    RES = models.Student.objects.order_by ( " ID " ) .all () 

    #Repeatedly press the age field may be ordered in descending order, if the same age, according to the id field ascending 
    RES = models.Student.objects.order_by ( " -AGE " , " id " ) .all () 

# 7. The Group by 
    # packet needs the method used in django 
    from django.db.models Import the Count, Max, Min, the Sum 
    RES = models.Student.objects.values ( " name " ) .annotate (XXX = the Count ( " ID " ))
     # represents the Search for the name field and table id field of computing count, and played an alias for the group by name xxx field 
   # the SELECT "app01_student." "name", COUNT ( "app01_student." "id") aS "xxx" the fROM "app01_student" GROUP BY "app01_student"."name"

# 8. The only take only a column value 
    RES = models.Student.objects.only ( " name " , " Age " ) .all ()
     # taken out is QuerySet which contains a list of objects, the object field contains the name and value id value of the column 
    # differences and values the value is only taken out of the list of sets of objects, a list of values is set to take the dictionary 
    # and no matter whether you only contain id field, he will put together to take your id field out 

# 9. the removed except the defer column is a column other values 
    RES = models.Student.objects.defer ( " ID " , " name " ) .all ()
     # taken out of the list of sets of objects are, and no matter which you there is no id field, you will get the value out of a column id 

# 10. the the using 
    # when we configure the database, the database will add to the dATABASES 
    # dATABASES = {
    #      'Default': { 
    #          'ENGINE': 'django.db.backends.sqlite3', 
    #          'NAME': the os.path.join (base_dir, 'db.sqlite3'), 
    #      } 
    # } 
    # if there have even multiple databases, can be determined by using a database which use 
    RES = models.Student.objects.all (). using ( " default " ) 

# 11. lookup table a total of how many data 
    RES = models.Student.objects .count () 

# 12. The first data 
    RES = models.Student.objects.first () 

# 13. Finally, a data 
    RES = models.Student.objects.last () 

# 14. a gte gt less than greater than greater than or equal lt lte less than or equal
    models.Student.objects.filter = RES (= id__gt. 8 ) .all () 

    RES = models.Student.objects.filter (= id__lte. 8 ) .all () 

# 15. A operation and, in the filter is separated by a comma and 
    RES = models.Student.objects.filter (ID = 2, Age = 23 is )
     # returns the type or QuerySet 

# 16. a operation or 
    # or Q methods need to import from the django 
    from django.db.models import Q 
    RES = Models .Student.objects.filter (Q (ID = 2) | Q (Age = 12 is )) 

    RES = models.Student.objects.filter (Q (Q (ID = 2) | Q (Age = 12 is)) & Q ( = name " Li Tiezhu " ))
     #    | representation or representation with & 

#17. On the basis of the original update, for example, I want the whole value of a column of 1 + 
    # need to import from the F method in django 
    from   django.db.models Import F 
    models.Student.objects.update (F ( " Age " ) + 1 )
     # such fields will all age + 1 

# 18. native sql statement 
    # Django also provides us a way to write the native sql 
    from django.db import connection     # introduction joint 
    cursor connection.cursor = ()     # generates cursor object 
    cursor.execute ()      # can be submitted sql statement, you can also pass parameters 
    cursor.fetchall ()    # pick up all the results 
    cursor.fetchone ()    #Pick up a result 
    
    models.Student.objects.raw ()     # This method can also be submitted sql statement does not recommend using 
copy the code 
 add and delete some of the supplementary 
copy the code 
# by 

# the first to add a data 
models.Student.objects.create ( = name " XXX " , Age = 12 is ) 

# a plurality of data increases 

obj = [ 
    models.Student (name = " QQQ " , Age = 12 is ), 
    models.Student (name = " AAA " , Age = 32 ), 
    Models .Student (name = " WWW " , Age = 21 is ) 
]
models.Student.objects.bulk_create (obj) 

# delete 

models.Student.objects.filter (name = " xxx " ) .Delete ()
 # If you delete a single table no problem, but if the primary key of this table and other tables to establish a foreign key relationship, delete this data, the corresponding data from another table will be deleted 
# this is also known as cascading deletes, so if we do not need, you can build a foreign key is assigned to on_delete 
# on_delete few meaning argument 
    # cASCADE defaults cascade delete 
    # SET_NULL cancel cascading deletes, if the data is associated with a foreign key is deleted, the corresponding value associated with the NULL in place, it needs the support of NULL 
    # set_default cancel cascading deletes, it is deleted Instead of using the default value 

class Student (models.Model): 
    name = models.CharField (MAX_LENGTH = 32 ) 
    TEAC = models.ForeignKey ( ' Teacher ', null=True, on_delete=models.SET_NULL)

 

Guess you like

Origin www.cnblogs.com/duGD/p/11203981.html