with that query I got an error: "Cannot resolve keyword 'dd' into field. Choices ....(the EE model fields)", because "EE" don't have a foreign key to "DD", DD have an foreign key to "EE". With "PPDD.objects.select_related().filter(user=2)" works, but that's not the idea, because the query can grow up and in production can be slow.
My idea is do a subquery like "DD.objects.filter(id__in=(PPDD.objects.filter(user=2)))" but that's not works because this is transformed in a query like this: SELECT ... FROM "DD" WHERE "DD"."id" IN (SELECT U0."id" FROM "PPDD" U0 WHERE U0."user_id" = 2) and i new something like this: SELECT ... FROM "DD" WHERE "DD"."id" IN (SELECT U0."departamento_id" FROM "PPDD" U0 WHERE U0."user_id" = 2) the problem is SELECT U0."id". For my requeriments must to be SELECT U0."departamento_id" And other question. In this query "EE.objects.filter(dd__ppdd__user=2)" how can I put the "related_name" when in a model are more than 1 foreign key to the same model? On 8 sep, 11:00, Daniel Roseman <dan...@roseman.org.uk> wrote: > On Sep 8, 3:25 pm, refreegrata <refreegr...@yahoo.com> wrote: > > > > > Hello list, I have question. With this model: > > --------------------------------------------------------------------- > > 1) class EE(Persona): > > ..... > > 2) class DD(models.Model): > > ee_id = models.ForeignKey(EE) > > > 3) The User table > > > 4) class PPDD(models.Model): > > user_id = models.ForeignKey(User) > > dd_id = models.ForeignKey(DD) > > ..... > > -------------------------------------------------------------------- > > > How can I do (without RawSQL) a query like this?: > > > select distinct EE.* from EE inner join > > ( > > select DD.* from DD inner join PPDD on (DD.id=PPDD.dd_id and > > PPDD.user_id=2) > > ) as my_filter > > on (EE.id=my_filter.ee_id) > > > I want to get the data in the table EE that's associated to PPDD > > through DD. I read the django documentation, but I can't understand > > how do the query. > > > Thanks for read, and sorry for my poor english. > > Firstly, don't call your foreignkey fields "foo_id". Django > automatically creates an underlying database field with the "_id" > suffix, but the ForeignKey field on the model refers to the linked > model instance itself, not the id. > > Anyway, assuming you've changed those names, the query is: > > EE.objects.filter(dd__ppdd__user=2) > > The double-underscore syntax is used for crossing joins. > -- > DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.