Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Django filter statement what's the difference between __in and equal sign (=)?

Tags:

python

django

Following this question, I'd like to know if there any difference in writing

.filter(league_pk__in=[1,2,3])

and

.filter(league=[1,2,3])

I have tried both and they seem to return the same results in my code but nowhere in the documentation does it say that they're the same thing (as with __exact).

Can I safely assume the two forms are equal? Where is it documented?

like image 854
Saturnix Avatar asked Dec 01 '15 18:12

Saturnix


1 Answers

Without knowing what the relation is defined on your models, we can't say for sure. What I can give you is a way to check the query, that will tell you for sure if they are equivalent or not:

print your_qs.filter(league_pk__in=[1,2,3]).query
print your_qs.filter(league=[1,2,3]).query

And look at the sql generated to see what's actually going on at the db level.

like image 182
wim Avatar answered Oct 24 '22 09:10

wim