In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
What is the use of Django object.get_or_create ()? 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.
Use get_or_create ()
Mode of use
User, b = User.objects.get_or_create (u_id=1, name= "Zhang San", defaults= {'address':' Shanghai'}) print (user)
Equivalent to
Users = User.objects.get (u_id=1, name= "Zhang San") if user: print (user) else: user = User.objects.create (u_id=1, name= "Zhang San", address= "Shanghai") print (user) instructions about get_or_create ()
Get_or_create, as the name implies, when querying or creating a piece of data, the first thing to do is to query whether there is such a value. If so, return it. If not, create it.
Get_or_create returns a tuple, with the first value being the data found or created, and the second value being a Boolean indicating whether the creation operation was performed.
Similar to using get queries, MultipleObjectsReturned is triggered when more than one piece of data is found.
Creating it is similar to creating it with create.
If you are using MySQL, be sure to use the READ COMMITTED isolation level instead of the default REPEATABLE READ, otherwise you will encounter a situation where get_or_create throws IntegrityError but the object does not exist in the next get () call.
After reading the above, have you mastered the method of using Django object.get_or_create ()? 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.