14👍
Adding to @Bernhard answer, other possible solution can be achieved using the Q()
object.
from django.db.models import Q
filters = Q(manytomany=None)
TestModel.objects.filter(filters)
Negation:
filters = ~Q(manytomany=None)
TestModel.objects.filter(filters)
- [Django]-How to 'bulk update' with Django?
- [Django]-Django change default runserver port
- [Django]-Django migration fails with "__fake__.DoesNotExist: Permission matching query does not exist."
2👍
Even though the topic has already an answer this could be of help. Try with lookups:
empty = TestModel.objects.filter(manytomany__isnull = True)
#........If you want to get their counter part
not_empty = TestModel.objects.filter(manytomany__isnull = False)
Basically, you get two query sets: one where your manytomany fields are empty, and the other with objects that have data in the manytomanyfield.
Hope this could be of some help!
- [Django]-Embedding JSON objects in script tags
- [Django]-Why is factory_boy superior to using the ORM directly in tests?
- [Django]-Django admin: how to sort by one of the custom list_display fields that has no database field
0👍
this is an old question but I needed it and the provided answers didn’t work for me, but I did fix it and got the proper filtering (on Django 2.2) this is how:
testModel.objects.filter(testmodel__anothermodel=None)
as you can see using the model name all lower case then two underscores then the many to many model name that did it for me
- [Django]-How do I reference a Django settings variable in my models.py?
- [Django]-How do I get user IP address in Django?
- [Django]-How to set the timezone in Django
Source:stackexchange.com