Last active
February 19, 2023 10:00
-
-
Save soukiassianb/7e835fe773b2d7c43375 to your computer and use it in GitHub Desktop.
Use python set to compare django querysets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
First technique, use set | |
""" | |
# two querysets or lists | |
q1 | |
>>> [<obj1>,<obj2>, <obj3>] | |
q2 | |
>>> [<obj4>,<obj2>, <obj5>] | |
# compare querysets : | |
set(q1) == set(q2) | |
>>> False | |
# get objects present in both | |
set(q1).intersection(set(q2)) | |
>>> <obj2> | |
# get objects of q1 not in q2 | |
set(q1).difference(set(q2)) | |
>>> <obj1> <obj3> | |
# vice versa | |
set(q2).difference(set(q1)) | |
>>> <obj4> <obj5> | |
""" | |
Second technique, use exclude / filter | |
""" | |
thank you maaan
Thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where is the second technique?