Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between get and filter methods in Django

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What is the difference between get and filter methods in Django, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

Get

Get is to get an object, and sometimes DoesNotExist: User matching query does not exist occurs.

One reason I encountered was that get didn't get the content because there was no data in the User table in the database.

Solution: fill in the relevant data in the User table, and then use the following code.

# solution one: getprofile_mail = User.objects.get (uid=uid) print (profile_mail) if not profile_mail: return Falseprint (profile_mail.mail) return JsonResponse (profile_mail.mail, safe=False)

What profile_mail gets is an object Object, and you need .mail to get mail.

The output is as follows:

User object (11) 123@qq.com

There is another mistake here: In order to allow non-dict objects to be serialized set the safe parameter to False.

Because the return is Json data, it needs to be serialized, so a safe=False is added to return JsonResponse (profile_mail.mail, safe=False).

Get returns an object and can only return one. If the record does not exist, it will report an error.

Filter

When faced with multiple objects, you can't use get, you should use filter instead.

Solution:

# solution two: postprofile_mail = User.objects.filter (uid=uid) print (profile_mail) for i in profile_mail: print (i.mail) return JsonResponse (i.mail, safe=False)

Filter returns a list of objects and returns [] if the record does not exist.

The output is as follows:

Values and values_listvalues of 123@qq.comfilter (* fields)

Returns a ValuesQuerySet (a subclass of QuerySet) that iterates with a dictionary that represents an object, but not a model instance object.

Profile_mail = User.objects.filter (uid=uid) print (profile_mail) profile_mail = User.objects.filter (uid=uid). Values () print (profile_mail)

The output is as follows:

Values () receives the optional position parameter * fields, which specifies which fields SELECT should restrict. For example, filter the mail information as follows:

Profile_mail = User.objects.filter (uid=uid) .values ('mail') print (profile_mail)

The output is as follows:

Values_list (* fields, flat=False)

A tuple is returned instead of a dictionary. Each tuple contains the value of the field passed to the values_list () call, so the first element is the first field, and so on.

Profile_mail = User.objects.filter (uid=uid). Values_list ('uid','mail') print (profile_mail)

The output is as follows:

If you pass only one field, you can also pass the flat parameter. If True, it indicates that the returned result is a single value rather than a tuple.

Profile_mail = User.objects.filter (uid=uid). Values_list ('mail', flat=True) print (profile_mail)

The output is as follows:

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report