whiskeyjuvenile kirjoitti:
> I'm making some sort of galactic map with the following classes
> defined in models.py:
> 
> class Star(models.Model):
>     name = models.CharField(max_length=200)
>     xcoord = models.FloatField()
>     ycoord = models.FloatField()
>     zcoord = models.FloatField()
>     def __unicode__(self):
>         return self.name + ' (' + str(self.xcoord) + ', ' + str
> (self.ycoord) + ', ' + str(self.zcoord) + ')'
> 
> class Satellite(models.Model):
>     name = models.CharField(max_length=200)
>     description = models.TextField(blank=True, null=True)
>     system = models.ForeignKey(System, related_name='satellites')
>     def __unicode__(self):
>         return self.name
> 
> class Planet(Satellite):
>     orbital = models.IntegerField()
> 
> class Moon(Satellite):
>     suborbital = models.IntegerField()
>     orbits = models.ForeignKey(Planet, related_name='moons')
> 
> What should I do so I can refer to an s.planets object containing all
> objects of class Planet with system==s?
> 
> it seems like Star should have added
>     def _get_planets(self):
>         return self.planets
>     def _set_planets(self, planet):
>         self.planets.append(planet)
>     planets = property(_get_planets, _set_planets)
> 
> and Planet should have something like
>     def __init__(self, *args, **kwargs):
>         super(Planet, self).__init__(*args, **kwargs)
>         self.star.planets.append(self)
>     def save(self):
>         super(Planet, self).save()
> 
> Does this look accurate?

No. It seems that there is something wrong. There is no relation between 
Star and Satellite, Planet or Moon. Releation is made between System (?) 
and Satellite and Satellite has one to one relation to Planet And Moon 
(comes from inheritance).

To get all Planets in system it should be something like:

Planet.objects.filter(system=s) and that's it. Nothing complicated. But 
to get all Planets from System is a bit more trickier since there 
doesn't exist that reverse relation directly.

So first you need to fetch all Systems that has Satellite that has 
Planet. something like 
System.objects.filter(satellites__planet_set__isnull=False)

A bit tricky. If you don't want to have own table for Satellite, make it 
to abstract and then Planet and Satellite both will be pure concrete 
classes (then of course you can't have single "satellites" but both 
inherited concrete classes do need their own reverse relations.

-- 
Jani Tiainen

--~--~---------~--~----~------------~-------~--~----~
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