I have these models set up in Django:
class SourceBusiness(models.Model):
source = models.CharField(max_length=100)
...(other fields)
class ResultBusiness(models.Model):
sourcebusiness = models.ForeignKey(SourceBusiness)
...(other fields)
I want to get a list of distinct sources for which objects exist in the ResultBusiness table. For example, for the following data,
a = SourceBusiness(source="A")
a1 = SourceBusiness(source="A")
b = SourceBusiness(source="B")
c = SourceBusiness(source="C")
x = ResultBusiness(sourcebusiness=a)
y = ResultBusiness(sourcebusiness=a1)
z = ResultBusiness(sourcebusiness=c)
I want to find that ResultBusinesses exist for sources a, a1 and c, and the sources for those SourceBusinesses are “A” and “C”. Ideally, I would like to get the [“A”, “C”] in one step. Is this possible with the Django QuerySet API?
If it’s not straightforward I can work around, just want to know if this can be done in one step.
EDIT: just to clarify, [“A”, “C”] is the result I want, not the input. So, not “which ResultBusiness come from SourceBusiness with source in [A,C]”, but “for all ResultBusinesses which exist, what are the distinct possible values of their parent SourceBusiness.source”
1 Answer