35👍
Since Django 1.11, QuerySets have a difference()
method amongst other new methods:
# Capture elements that are in qs_all but not in qs_part
qs_diff = qs_all.difference(qs_part)
22👍
You should be able to use the set operation difference to help:
set(alllists).difference(set(subscriptionlists))
- [Django]-How to get user permissions?
- [Django]-How do I use Logging in the Django Debug Toolbar?
- [Django]-Which Stack-Overflow style Markdown (WMD) JavaScript editor should we use?
11👍
Well I see two options here.
1. Filter things manually (quite ugly)
diff = []
for all in alllists:
found = False
for sub in subscriptionlists:
if sub.id == all.id:
found = True
break
if not found:
diff.append(all)
2. Just make another query
diff = List.objects.filter(datamode = 'A').exclude(member__id=memberid, datamode='A')
- [Django]-How can one use enums as a choice field in a Django model?
- [Django]-How can I correctly set DJANGO_SETTINGS_MODULE for my Django project (I am using virtualenv)?
- [Django]-Django/Python assertRaises with message check
4👍
How about:
subscriptionlists = Membership.objects.filter(member__id=memberid, datamode='A')
unsubscriptionlists = Membership.objects.exclude(member__id=memberid, datamode='A')
The unsubscriptionlists should be the inverse of subscription lists.
Brian’s answer will work as well, though set() will most likely evaluate the query and will take a performance hit in evaluating both sets into memory. This method will keep the lazy initialization until you need the data.
- [Django]-How to add the custom button which executes a Django admin action to change form page?
- [Django]-How to do a HTTP DELETE request with Requests library
- [Django]-Django required field in model form
0👍
In case anyone’s searching for a way to do symmetric difference, such operator is not available in Django.
That said, it’s not that hard to implement it using difference
and union
, and it’ll all be done in a single query:
q1.difference(q2).union(q2.difference(q1))
- [Django]-Django model blank=False does not work?
- [Django]-ProgrammingError: relation "django_session" does not exist error after installing Psycopg2
- [Django]-Is it safe to rename Django migrations file?
Source:stackexchange.com