Python django how to check if query result returned result

I did not have very thorough training in python, and sometimes I do not know how to do it correctly. One of these things is testing if the result of resultQuery returned the result or not. I do a lot:

try: user = User.objects.get(email=email) except DoesNotExist: user = User() 

I don't know about python, but trying to catch in other languages โ€‹โ€‹should be for exceptions, and not for a normal program flow. How would you do this with if else? I think I want something similar to:

 if request.GET.get('email','') is not None: email = request.GET['email']) 
+4
source share
4 answers

Your example exception is most often the preferred way to do things. Wikipedia has a pretty nice description:

The Python style causes the use of exceptions whenever an error condition occurs. Instead of checking access to a file or resource before actually using it, in Python, just go ahead and try to use it by catching an exception if access is denied.

Exceptions are often used as an alternative to an if block (...). The accepted motto is EAFP, or "Easier to ask" for Forgiveness than Permission. "

Exceptions are not necessarily expensive in Python. Again, quoting the Wikipedia example:

 if hasattr(spam, 'eggs'): ham = spam.eggs else: handle_error() 

... vs:

 try: ham = spam.eggs except AttributeError: handle_error() 

These two code examples have the same effect, although there will be differences in performance. When spam has attribute eggs, the EAFP sample will run faster. When spam has no attribute eggs (an โ€œexceptionalโ€ case), the EAFP sample will run slower.

In particular, for Django I would use an exception example. This is the standard way to do something in this area, and the following standards will never be bad :).

+4
source

You can use .filter() instead of .get() , and also use an optimized django request rating.

You can do

 qs = User.objects.filter(email=email) if qs : user = qs[0] ... 
0
source

Django defines an EmptyQuerySet class that can be used for special implementations with rigorous testing of isinstance(...) .

A simple way is the following:

 try: user = User.objects.get(email=email) if user: # ... else: # ... except DoesNotExist: user = User() 

Django provides a get method to directly fetch a single instance of a model or throw an exception. In this case, the usual way is to check for exceptions like DoesNotExist

To get entire rows from a database query, as usual, use the user.objects.filter(...) method. The returned QuerySet instance provides you with .count() , .exists() methods.

0
source

I agree that catching an exception is the normal pattern here. If you need to check if a query returns a query, the exists method is one way to make sure that I don't think it has been mentioned yet. Documents go through various performance implications, especially if you know that the request will be evaluated, it is better to simply check the Boolean interpretation of the set of requests.

0
source

Source: https://habr.com/ru/post/1501096/


All Articles