Wednesday, 2 June 2021

Django: What's the difference between Queryset.union() and the OR operator?

When combining QuerySets, what's the difference between the QuerySet.union() method and using the OR operator between QuerySets |?

Consider the following 2 QuerySets:

qs1 = Candidate.objects.filter(id=1)
qs2 = Candidate.objects.filter(id=2)

How is qs1 | qs2 different from qs1.union(qs2)? Is there some subtlety under the hood that I'm missing?



from Django: What's the difference between Queryset.union() and the OR operator?

No comments:

Post a Comment