In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What is the query sentence of ORM in django? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
In daily development, there are too many query requirements in database addition, deletion and modification (CDUR), so the syntax of query is much more than add, delete and modify operations, especially cross-table association queries, which can make the code much simpler.
Go straight to the code. I rewrote it directly in the view function I wrote last time.
Def orm_test (request): "" add operation "" # add a class named 1901. Create is a new method, in which you can accept multiple field parameters # Class.objects.create (name= "1901") # both a student and a class action # add a class named 1903 Return a class instance # cls_instance = Class.objects.get (name= "1903") # the key on the left of stu_info corresponds to the Student model field, because cls is a foreign key So correspond to an instance of a class # stu_info = {# "name": "XIAOmei", # "age": "24", # "score": "88", # "email": "1333@qq.com", # "introduce": "if you are xiaomei" # "cls": cls_instance #} # Student.objects.create (* * stu_info) "" modify operation "# change the name of the class named 1901 to 1901_xiu Filter is filtering and supports multiple parameters. Update is an update method that supports multiple parameters # Class.objects.filter (name= "1901") .update (name= "1901_xiu") "" delete operation "" # delete the class named 1901_xiu Delete is the deletion method # Class.objects.filter (name= "1901_xiu"). Delete () "" query operation "# query a single item, and get returns an instance. If the query result is not incorrect, the result returned by the # filter query is a list of multiple instances, # instance = Student.objects.get (pk=1) # instance = Student.objects.filter (pk=1). First ()
# query multiple entries and return queryset type (list of multiple query result instances) can be iterated # queryset = Student.objects.all () # # for stu in queryset: # student's name, student's age student's score # print (stu.name,stu.age,stu.score)
# slice the result of the query set, take the 0th to the 4th, and return the executed sql statement like the slice of the list # queryset = Student.objects.filter (). All () [: 5] # limit statement # print (queryset.query) #
# precisely find the students whose names are in the same position as the two statements, and return the query set # queryset = Student.objects.filter (name=' Xiaomei') # queryset = Student.objects.filter (name__exact=' Xiaomei')
# ignore case # queryset = Student.objects.filter (name__iexact='xiaomei')
# Fuzzy query # queryset = Student.objects.filter (name__contains='xiao') # do not ignore case # queryset = Student.objects.filter (name__icontains='xiao') # ignore case
# regular matching method # queryset = Student.objects.filter (name__regex=' ^ x') # queryset = Student.objects.filter (name__iregex=' ^ x')
# greater than less than # queryset = Student.objects.filter (age__gt=17, age__lt=19)
# in all # queryset = Student.objects.filter in a collection (age__in= (18,17,16))
# queryset = Student.objects.all (). Order_by ('age') ascending # queryset = Student.objects.all (). Order_by ('-age') # ascending # queryset = Student.objects.all (). Order_by ('- age','id') # ascending # for stu in queryset: # print stu.age,stu.id
# specified field query # the first # queryset = Student.objects.values ('name','age'). All ()
# second # queryset = Student.objects.values_list ('name','age','score'). All ()
# query with tables # the first # queryset = Student.objects.all () # for stu in queryset: # print stu.name,stu.cls.id,stu.cls.name
# second: strong double underscore (across tables), you can use multiple double underscores across multiple tables # sentence function is to query the names of all students in the student table and the names of the students' class # cls__name is the cls double underscore name,cls is the cls field in Student, and name is the name field # queryset = Student.objects.values ('name','cls__name'). All ()
# query all the students in a class and execute the sql statement twice # cls1 = Class.objects.get (name='1701') # queryset = Student.objects.filter (cls=cls1)
# query all the students in a class and execute the sql statement # queryset = models.Student.objects.filter (cls__name='1903'). All (). Values ('name','cls__name')
# query all the students in a class. The starting table is queried from the class table. The reverse query # stu_cls is the alias of the foreign key in the student table # queryset = Class.objects.get (name='1903'). Stu_cls.all ()
# query more than 90 points by score # queryset = Student.objects.filter (score__gt=90). All ()
Return HttpResponse ('database operation succeeded') after reading the above, have you mastered what the query statement of ORM in django is? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.