views:

17

answers:

1

How do I express this SQL query in a Django Queryset?

SELECT * FROM Table1, Table2 WHERE Table1.id_table2 = Table2.id_table2;

Be aware that the structure of table1 implyes a id_table2 foreign key...

Why? Because I want to replace the id_table2 in the Table1 table1.object.all() listing with values asociated to the register involved in the relation. Like this

Whithout relationship

| id_table1 | id_table2 | foo_field1 | bar_field1 |
---------------------------------------------------
| 1         | 1         | foo1       | foo2       |
---------------------------------------------------

With Relationship

| id_table1 | foo_field2*| foo_field1 | bar_field1 |
----------------------------------------------------
| 1         | foo2       | foo1       | foo2       |
----------------------------------------------------

*: foo_field2 means not only this related field, but all related fields of that register.

Thanks in advance!!!!

A: 

You cannot perform that query with the Django ORM. The best you can do is use select_related() to also pull the fields for the other table at the same time:

Table1.objects.select_related('table2')
Ignacio Vazquez-Abrams