M
M
maksam072020-08-11 07:26:04
Django
maksam07, 2020-08-11 07:26:04

How to get all the rows in the third table, accessing the first through transit (foreign keys)?

There is a model like:

class A( models.Model ):
  field = models.CharField()

class B( models.Model ):
  a = models.ForeignKey( A, on_delete = models.CASCADE, related_name = 'a_b_a' )

class C( models.Model ):
  b = models.ForeignKey( B, on_delete = models.CASCADE, related_name = 'b_c_b' )

Referring to table "A": It is
c_all = A.objects.all()
necessary to add a query so that all rows from table "C" are pulled out. Table 'B' can have multiple rows related to 'A', just as table 'C' can have multiple rows related to any row in 'B'.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2020-08-11
@maksam07

c_all = C.objects.filter(b__a_id=123)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question