Consider the models:
#Models
class A(models.Model):
fieldOfA = models.CharField(max_length = 4)
class B(models.Model):
fieldOfB = models.CharField(max_length = 4)
class C(models.Model):
classA = models.ForeignKey(A, blank=True, null=True)
classB = models.ForeignKey(B, blank=True, null=True)
When I create objects of C, I ensure that an object has EITHER a classA or classB relationship.
I am looking for a single queryset, which gets me objects of C for specific fieldOfA or specific fieldOfB values.
I tried this, but it fails (returns [], despite there being valid results).
#Views - assume double underscore in the query
from django.db.models import Q
my_query = C.objects.filter(Q(classA _ _isnull = False, classA _ _fieldOfA = 'foo') | Q(classB _ _isnull = False, classB _ _fieldOfB = 'foo'))
The problem I see is the '|' that is the applied. Two different querysets for classA and classB work fine. Any way I could apply a single queryset to make this work? Or worse, a way to merge the individual querysets.