Reputation: 904
Here are my models:
class A(models.Model):
#Some fields
class B(models.Model):
#Some fields
class C(models.Model):
a = models.ForeignKey(A)
b = models.ForeignKey(B)
#Some more fields
class Meta:
unique_together = ['a', 'b']
Now based on some object of A (say 'a') , I want set of all 'b' for which C.a = 'a' and C.b = 'b' .
In other words, get a set of objects of B for which entries exist in a.c_set
Currently the way I am doing this is:
qset_c = C.objects.filter(a='a')
qset_b = [c.b for c in qset_c]
First of all this gives me a list (a queryset would be preferred).
Second, I am concerned about line 2 having list comprehension , as it may take a lot of time for huge data.
Is there a better way to achieve this?
Note: I understand , the statement is a bit unclear. If someone can edit it to make it more clear, it will be a lot helpful
Upvotes: 0
Views: 30
Reputation: 3308
Would this do what you want?
set_c = C.objects.filter(a=a).values_list('b', flat=True)
Upvotes: 1