[Answer]-Do two INNER JOIN's with DJANGO and retrieve variable(s)

1👍

The thing is that there isn’t a “the” Table3.name, because you have what is effectively a many-to-many relationship. For each instance of Table1, you have many Table3 instances, and vice versa.

However, it is fairly simple to get all the Table3.name values for a single Table1 instance.
The easiest way would be to add an explicit declaration for the many-to-many relationship – note that this is a logical field only, and does not require any schema changes.

class Table1(models.Model):
    ...
    table3s = models.ManyToManyField('Table3', through='Table2')

now you can do:

my_table_1.table3s.values_list('name', flat=True)

(I’m assuming your declaration of Table2 is supposed to have a FK to Table3, not back to itself, and that this is simply a mistake in the example.)

Leave a comment