Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between using 'and' and using '&' in Django ORM

I have Query which shows different results when i use and or &

criteria1 = Q(id__gte=802, id__lte=1000)
criteria2 = Q(country_id__contains='UK')

I always have been using :

q = Mymodel.objects.filter(criteria1 & criteria2)

But in this particular case when i use & it always outputs a single row . (I also checked print q.query(), query comes out to be fine)

However, when i use and instead of &. Query gives correct output

q = Mymodel.objects.filter(criteria1 and criteria2)

what actually is happening under the hood ?

like image 725
Mahender Thakur Avatar asked Feb 13 '19 12:02

Mahender Thakur


People also ask

What is the difference use and using?

Use vs Usage It is not correct to interchange them since they are different in their meanings. The word use is used in the sense of 'employ'. On the other hand, the word usage is used in the sense of 'practice' or 'convention' or 'the act of using something'. This is the main difference between the two words.

What is the using difference of with and by?

We use by to show how someone does something but we use with to show the tool or object used to do something: I made this cake by hand. I made this cake with the oven.


1 Answers

The correct one is criteria1 & criteria2. criteria1 and criteria2 uses standard Python boolean logic and will be evaluated into criteria2, while criteria1 & criteria2 uses overloaded __and__ method and constructs correct compound Q object:

In [1]: from django.db.models import Q                                                                                                                                                                                                                                                                                                                        

In [2]: criteria1 = Q(id__gte=802, id__lte=1000)                                                                                                                                                                                                                                                                                                              

In [3]: criteria2 = Q(country_id__contains='UK')                                                                                                                                                                                                                                                                                                              

In [4]: criteria1 & criteria2                                                                                                                                                                                                                                                                                                                                 
Out[4]: <Q: (AND: ('id__gte', 802), ('id__lte', 1000), ('country_id__contains', 'UK'))>

In [5]: criteria1 and criteria2                                                                                                                                                                                                                                                                                                                               
Out[5]: <Q: (AND: ('country_id__contains', 'UK'))>
like image 186
awesoon Avatar answered Oct 24 '22 03:10

awesoon