adelaide_mike wrote:
> Hi
> My models are, essentially :
> class Cable(models.Model):
>     cable = models.CharField(max_length=8)
> 
> class Connector(models.Model):
>     connector = models.CharField(max_length=8)
> 
> class Node(models.Model):
>     cable = models.ForeignKey(Cable)
>     connector = models ForeignKey(Connector)
> 
> So, a real world cable can be plugged in to no, one, or two connectors
> (one at each end).  The Node table describes the junctions between
> cables and connectors.
> 
> Can I, without resorting to raw SQL, build a queryset that shows
> connector-cable-connector?

You would have a hard time even accomplishing that in raw SQL with those 
models...  If you have the flexibility to change your models I would try 
something like:

class Connector(models.Model):
   name = models.CharField(max_length=8)

class Cable(models.Model):
   name = models.CharField(max_length-8)
   left_connector = models.ForeignKey(Connector, blank=True, null=True)
   right_connector = models.ForeignKey(Connector, blank=True, null=True)

This makes your 0, 1, or 2 relationship explicit. I went with the 
left/right distinction as it is an easy "handed" way of describing them, 
but other choices may be more appropriate depending on your usage 
patterns...

Otherwise, your best bet would seem to me to be to use 
Node.objects.all().orderby('cable') and group the 
connector-cable-connector by yourself. Something that I generally find 
useful in situations like that is Python's groupby function from itertools.

--
--Max Battcher--
http://worldmaker.net


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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
-~----------~----~----~----~------~----~------~--~---

Reply via email to