On Mar 18, 2011, at 6:38 AM, Ricardo F. Teixeira wrote:

> Hi everyone!
> 
> Without using raw SQL queries how can I make multiple joins using this
> model?
> 

Do a search for  "django select_related reverse one to many" for some 
alternatives

> I'm trying to get something like this:
> 
> SELECT Package.package, Release.release, Release.version,
> ContribComment.*
> FROM ContribComment
> INNER JOIN Contrib
> ON Contrib.id = ContribComment.fk_contrib_id
> INNER JOIN ContribRelease
> ON ContribRelease.fk_contrib = Contrib.id
> INNER JOIN Release
> ON Release.id = ContribRelease.id
> INNER JOIN Package
> ON Package.id = Release.fk_package
> 
> Is that possible to anchive something like this without using raw SQL
> queries?
> I'm asking that because I can't find any example with multiple inner
> joins that is similar to my model.
> 

What you want work is:
        Package.objects.select_related()

But since the ORM will only follow 'forward' ForeignKey references,
the result will be the same as:
        Package.objects.all()

You can get close with 
        ContribRelease.objects.select_related()

which will inner join everything except ContribComment since there is no
"forward" ForeignKey reference

see http://docs.djangoproject.com/en/dev/ref/models/querysets/#select-related


> class ContribComment(models.Model):
>       fk_contrib_id = models.ForeignKey(Contrib)
>       subject = models.TextField()
>       text = models.TextField()
>       ratings = models.IntegerField()
> 
> class Contrib(models.Model):
>       username = models.TextField()
>       city = models.TextField()
> 
> class ContribRelease(models.Model):
>       fk_contrib = models.ForeignKey(Contrib)
>       fk_release = models.ForeignKey(Release)
> 
> class Release(models.Model):
>       fk_package = models.ForeignKey(Package)
>       release = models.TextField()
>       version = models.TextField()
>       distribution = models.TextField()
>       arch = models.TextField()
>       summary = models.TextField()
>       description = models.TextField()
> 
> class Package(models.Model):
>       name = models.TextField(unique=True)
>       homepage = models.TextField()
>       snapshot = models.TextField()
> 
> Thanks,
> 
> Ricardo F. Teixeira
> 

Jason

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to