ORM database query operation API in the Django view, and django view ormapi

Source: Internet
Author: User

ORM database query operation API in the Django view, and django view ormapi
APIS for querying table records

Operation: models. Table name. objects. Method ()

All (): Query all results filter (** kwargs): it contains the object get (** kwargs) that matches the given filter condition ): returns an object that matches the given filtering condition. Only one of the returned results is returned. If more than one object meets the filtering condition, an error is thrown. Exclude (** kwargs): contains the values (* field) object that does not match the given filter condition: returns a ValueQuerySet -- a special QuerySet, what we get after running is not a series of model instantiation objects, but an iterative dictionary sequence values_list (* field): It is very similar to values (), and it returns a sequence of tuples, values returns a dictionary sequence order_by (* field): sorts query results by reverse (): sorts query results by reverse Order by distinct (): removes duplicate records from the returned results by count (): returns the number of objects that match the query (QuerySet) in the database. First (): returns the first record last (): returns the last record exists (): returns True if QuerySet contains data; otherwise, returns False

Note: object and querySet must be distinguished !!!

Object, which can call internal methods

Queryset is a set.

Single Table query with double underscores
Models. tb1.objects. filter (id _ lt = 10, id _ gt = 1) # obtain the value models with id greater than 1 and less than 10. tb1.objects. filter (id _ in = [11, 22, 33]) # obtain data models with IDs equal to 11, 22, and 33. tb1.objects. exclude (id _ in = [11, 22, 33]) # not in models. tb1.objects. filter (name _ contains = "ven") models. tb1.objects. filter (name _ icontains = "ven") # icontains case insensitive models. tb1.objects. filter (id _ range = [1, 2]) # range: bettwen and startswith, istartswith, endswith, iendswith

  

Example: def query (request): # query method API: #1 all method: models. table name. objects. all () book_all = models. book. objects. all () # The result is the querySet set # print (book_all) #2 filter: models. table name. objects. filter () ret = models. book. objects. filter (title = 'php') # The result is querySet set ret2 = models. book. objects. filter (nid = 1) # The result is querySet set ret3 = models. book. objects. filter (author = 'Alex ', price = 35) # The result is a set of querysets and the relationship between them. Both conditions must meet # print (ret) #3 get: models. table name. objects. get () ret4 = models. book. objects. get (nid = 3) # model object. If no value is obtained, an error is returned. # print (ret4, ret4.price) #4 exclude: exclusion condition. The value is not ret5 = models. book. objects. exclude (author = 'oldboys') #5 values method # ret6 = models. book. objects. filter (author = 'Alex '). values ('title', 'price') # print ('ret6 ', ret6) #6 values_list method # ret7 = models. book. objects. filter (author = 'Alex '). values_list ('title', 'price') # print ('ret7 ', ret7) # ret8 = models. book. objects. filter (author = 'Alex '). values ('author '). distinct () # print ('ret8 ', ret8) # ret9 = models. book. objects. filter (price _ gt = 30) ret10 = models. book. objects. filter (title _ startswith = 'P') ret11 = models. book. objects. filter (id_lt = 4, id_gt = 2) # id greater than 2 less than 4 print ('ret10 ', ret10) return HttpResponse ("OK ")

  

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.