Explanation of Common Data Query Methods and Query Object Conditions in Django

  • 2021-11-29 07:45:24
  • OfStack

(1) Common query methods:

1. Get all records:

s = User.objects.all()

2. Access to Article 1 data:

s = User.objects.first()

3. Get the last piece of data:

s = User.objects.last()

Note on 2. 3 Two methods:

For QuerySet object, we can also get the corresponding instance object through subscript value. Here's what it looks like--although both methods are useful (to get the corresponding instance object), the recommended (and generally used) methods are first () and last ().

Reason-If the queried data does not exist, using subscript query will report an error; However, using first () method will not report an error, but will return None.

s = User.objects.filter(age=18).first()     # Get QuerySet The first of the 1 Bar data 
s2 = User.objects.filter(age=18).last()     # Get QuerySet The last of the 1 Bar data 
s = User.objects.filter(age=18)[0]

Application scenario:

a = User.objects.get(id=56)             # Use get Method gets 1 Instance objects, we can guarantee that id It won't be repeated, but there is no guarantee id For 56 Does the data of exists 
# But using .first() Method will not report an error even if it does not exist! 
a = User.objects.filter(id=56).first()

4. Obtain filtered records according to the conditions provided by parameters:

[Note: filter (**kwargs) Method: Obtain a filtered QuerySet according to the extraction conditions provided by the parameters]

s = User.objects.filter(name= "xiaoming" )	#  Filter to obtain name For xiaoming Data of 

5. Data excluding name as xiaoming:

s = User.objects.exclude(name='xiaoming')

6. Get a record object:

"Note: The object returned by get has the property of uniqueness. If there are multiple eligible objects, get will report an error!"

s = User.objects.get(name = "xiaoming" )

7. Sort the results:

# According to age Ascending sort: 
c = User.objects.all().order_by("age")
# According to age Reverse sort: 
c2 = User.objects.all().order_by("-age")
# Double sort: age Sort in ascending order, if any age The same is with id Ascending sort: 
c3 = User.objects.all().order_by("age","id")

8. Convert the Model in the returned QuerySet to a dictionary:

s = User.objects.all().values()
# Function: It is now in dictionary form (before QuerySet Is an object similar to a list), you can use a dictionary method to get data: 

QuerySet object format:

s = User.objects.first()

Format transformed using the values () method:

s = User.objects.first()

9. Get the total number of data currently queried:

s = User.objects.first()

(2) Common query object conditions:

The condition of finding an object means 1 parameters passed to the above method. Equivalent to the where statement in the SQL statement after the condition, syntax for the field name __ rule!

1. exact is equivalent to an equal sign: (The following two methods work 1! )

rs = User.objects.filter(name__exact='xiaoming')
rs = User.objects.filter(name='xiaoming')

2. iexact: Similar to exact, except that case matches are ignored.

3. contains contains:

s = User.objects.first()

4. icontains is similar to contains, except that case is ignored.

5. What does startwith begin with:

s = User.objects.first()

6. istartwith is similar to startwith, except that case is ignored.

7. What does 7. endwith end with?.

8. isendwith is similar to endwith except that case is ignored.

9. Members of 9. in belong to:

s = User.objects.first()

10. gt is greater than

s = User.objects.first()

11. gte is greater than or equal to.

12. lt is less than.

13. lte is less than or equal to.

14. range interval with two ends.

rs = User.objects.filter(age__range=(18,20))  #  Query age Value in 18-20 Data between (including 18 And 20 ) 

15. isnull determines whether it is null.


Related articles: