Django-model foundation -- query table records

Query related API

<1> all(): Query all results
 QuerySet 
book_l =models.Book.objects.all() <models-->models.py | objects-->Manager>

 
<2> filter(** kwargs): It contains objects that match the given filter conditions
 QuerySet            <result set that matches> 
book_l =models.Book.objects.filter(price=134 )

 
<3> get(** kwargs): Returns objects that match the given filter conditions, and returns one and only one result,
                           An error is thrown if more than one or none of the objects match the filter criteria.
Object
models.Book.objects.get(title="数学书")

 
<5> exclude(** kwargs): It includes objects that do not match the given filter conditions.
 QuerySet 
book_l =models.Book.objects.exclude(price=134 )

 
<4> values(* field): Returns a ValueQuerySet - a special QuerySet that does not get a series of
                           An instantiated object of model, but an iterable sequence of dictionaries 
QuerySet 
models.Book.objects.all().values( " title " , " price " )

 
<9> values_list(* field): It is very similar to values(), it returns a sequence of tuples 
QuerySet 
models.Book.objects.all().values_list( " title " , " price " )

 
<6> order_by(* field): sort the query results
 QuerySet 
models.Book.objects.all().order_by( " price " ) <sort from small to large>

models.Book.objects.all().order_by( " -price " ) <order from big to small>


 
<7> reverse(): Reversely sort the query results
 QuerySet 
models.Book.objects.all().order_by( " price " ).reverse() <sort from big to small>

 
<8> distinct(): Eliminate duplicate records from the returned
 QuerySet

 
<10> count(): Returns the number of objects matching the query (QuerySet) in the database.
return value
book_list.count()

 
<11> first(): Return the first record
 Object 
book_obj = models.Book.objects.all().first()

 
<12> last(): Return the last record
 Object 
book_obj =models.Book.objects.filter(title= " language book " ).last()


<13> exists(): Returns True if the QuerySet contains data, otherwise returns False
ANS=models.Book.objects.all().exists()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325347975&siteId=291194637