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.