I have a hierarchy of models where the bottom object in the hierarchy has a
many to many relationship to itself.
Here are my models and views:
models.py
class Bar(models.Model):
name = models.CharField(max_length=200)
class BarPart(models.Model):
bar = models.ForeignKey(Bar)
name = models.CharField(max_length=255)
class BarComponentVersion(models.Model):
name = models.CharField(max_length=255)
barcomponentversions = models.ManyToManyField('self')
views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.db import models
from foo.bar.models import Bar, BarPart, BarComponentVersion
def all(request):
query = '10'
all =
Bar.objects.filter(barpart__barcomponentversion__barcomponentversions__barpart__bar__id=query).distinct()
sql, cols = all.query.as_sql()
c = RequestContext(request, {'all': all, 'sql': sql})
return render_to_response("foo/all.html", c)
When I run the query seen in the view, it works how it should, and generates
this query:
SELECT DISTINCT `foo_bar`.`id`, `foo_bar`.`name` FROM `foo_bar` INNER JOIN
`foo_barpart` ON (`foo_bar`.`id` = `foo_barpart`.`bar_id`) INNER JOIN
`foo_barcomponentversion` ON (`foo_barpart`.`id` =
`foo_barcomponentversion`.`barpart_id`) INNER JOIN
`foo_barcomponentversion_barcomponentversions` ON
(`foo_barcomponentversion`.`id` =
`foo_barcomponentversion_barcomponentversions`.`from_barcomponentversion_id`)
INNER JOIN `foo_barcomponentversion` T5 ON
(`foo_barcomponentversion_barcomponentversions`.`to_barcomponentversion_id`
= T5.`id`) INNER JOIN `foo_barpart` T6 ON (T5.`barpart_id` = T6.`id`) WHERE
T6.`bar_id` = %s
What this does is it goes FROM a specific bar to all the other bars that are
related to it.
I also want to go TO a specific bar from all the other bars it is related
to.
That means I want this query:
SELECT DISTINCT P2.`id`, P2.`name` FROM `foo_bar` P1 INNER JOIN
`foo_barpart` ON (P1.`id` = `foo_barpart`.`bar_id`) INNER JOIN
`foo_barcomponentversion` ON (`foo_barpart`.`id` =
`foo_barcomponentversion`.`barpart_id`) INNER JOIN
`foo_barcomponentversion_barcomponentversions` ON
(`foo_barcomponentversion`.`id` =
`foo_barcomponentversion_barcomponentversions`.`from_barcomponentversion_id`)
INNER JOIN `foo_barcomponentversion` T5 ON
(`foo_barcomponentversion_barcomponentversions`.`to_barcomponentversion_id`
= T5.`id`) INNER JOIN `foo_barpart` T6 ON (T5.`barpart_id` = T6.`id`) INNER
JOIN `foo_bar` P2 ON (T6.`bar_id` = P2.`id`) WHERE P1.`id` = %s
Essentially what I need to do is change the returned values to the SECOND
bar, and change the where clause to the FIRST bar.
Is there a way to do this with the ORM? Or should I just use the straight
SQL query?
Jacob
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---