In django, I want to get objects from a database depending on the attributes of some other objects. If one of the other objects does not exist, it should not affect the result of the request. The code looks like this:
from django.db.models import Q
try:
objectA = MyModel.objects.get(id = idA)
qA = Q(foo = objectA.bar)
except MyModel.DoesNot.Exist:
qA = Q(???)
try:
objectB = MyModel.objects.get(id = idB)
qB = Q(abc = objectB.xyz)
except MyModel.DoesNot.Exist:
qB = Q(???)
result = MyOtherModel.objects.filter(qA | qB, **other_filter_conditions)
For Querysets, there is a method none()that always returns an EmptyQueryset. Is there something similar for Q objects?
Or is there a better way to solve my problem?
source
share