In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "what is the difference between the return value of get () and filter () in Django", so the editor summarizes the following content to you, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "what is the difference between the return value of get () and filter () in Django".
Go to the official document first!
Filter (* * kwargs)
Returns a new query set that contains objects that match the given lookup parameters.
To put it simply, return a collection of queries made up of objects
Get (* * kwargs)
Returns an object that matches the given lookup parameter in the format described in the field lookup.
Examples
For example, there is an Order class in Model that contains an id field, and enter an id whose id is the 2019 field.
1.get () method
Orders = Orders.objects.get (id=20190003) print (order)
Check what orders is first, and the result is
Orders object
According to the official document, he is an object, yes, you can directly use a point operation to take the value of the field.
Print (order.id)
The result is 20190003.
The get method of 1. 1 django takes a matching result from the database and returns an object that reports an error if the record does not exist.
For example, there is a record in my database, and the value of the recorded name is "Lao Wang python". I use student= Student.objects.get (name=' Lao Wang python') to return a record object, which can be viewed through student.__dict__. It returns the form of a dictionary. {'key':valeus}, key is the name of the field, and values is the content of the value. If I use the get method to query a record that does not exist in the database, the program will report an error. For example: student = Student.objects.get (name=' Lao Wang').
1.2 if you use the get of django to get the data of the associated table, and if the data of the key table is more than 2 items, it will also report an error.
2.filter () method
Orders = Orders.objects.filter (id=20190003) print (order)
Check what orders is first.
I think the previous QuerySet tells us that this is a query set, and the real content of the query set is the contents of the list [], indicating the scope of the query set. The same goes for Orders in []. At this point, we find that what is in [] is what the get () method gets, so we just need to take order [0] and print order [0] to see it.
Orders object
Therefore, order [0] .id. You can get 20190003.
Note: if the filter query returns more than one result, the result is
It essentially looks like a list.
Add: the filter method of 2.1django gets matching results from the database and returns a list of objects. If the record does not exist, it returns [].
For example, there is a record in my database. If the value of name recorded is Lao Wang python, I use student = Student.objects.filter (name=' Lao Wang python'). The student returned by it is a list of objects. You can see that the result of student [0] is the same as the student returned by get above.
2.2 if you use django's filter to get the data of the associated table, no matter how many records there are in the associated table, it will not report an error.
In addition, I see from other materials that filter seems to have the function of caching data, querying the database for the first time and generating the cache, and the next time you call the filter method, you will directly get the cached data, and the get method will query the database directly every time. I don't know if this is correct.
The above is about the content of this article on "what is the difference between the return values of get () and filter () in Django?" I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
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.