Django OR querysets
One of Django's cool (maybe the coolest) and undocumented feature is being able to OR and AND queries using bitwise (&|) operators. It is a variation to the Q object that is documented.
for r in (
MyTable.objects.filter(somefield='a') |
MyTable.objects.filter(someotherfield='b')
).filter(somethird='c'):
print r
Now this becomes really useful when you use related_managers:
for r in (
aTableinstance.mytable_set.all() |
xTableinstance.mytable_set.all()
):
do_something_with(r)
while aTable and xTable are totally different instances yet they reference MyTable
Written by Hatem Nassrat
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#