Mohamed Ali
Mohamed Ali

Reputation: 465

How to create a list of related fields from the queryset result in django?

I would like to create a queryset from another one which is obtained from a filter search query.

There are my models :

class A(models.Model):
    b   = models.ForeignKey(B)
    c   = models.ForeignKey(C)
    score   = models.FloatField(default=5) 

My first query is to filter A objects related to a particular B instance "b_instance":

a_list  = A.objects.filter(b=b_instance)

How can I obtain form "a_list", a "c_list" regrouping all "c" fields objects without refer to make a loop ?

Upvotes: 0

Views: 196

Answers (1)

hqtay
hqtay

Reputation: 430

c_list = a_list.values_list('c', flat=True)

would give a list of all the c values from the filtered A objects

Upvotes: 1

Related Questions