I have a situation which is parallel to the following:

class Building(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100)
    def __unicode__(self):
        return self.name

class Manufacturer(models.Model):
    name = models.CharField(max_length=100)
    def __unicode__(self):
        return self.name

class Alarm(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100)
    building =  models.ForeignKey(Building)
    master = models.BooleanField(default=0)
    manufacturer = models.ForeignKey(Manufacturer)
    def __unicode__(self):
        return self.name

Now I need to display a list of all buildings, along with any alarm
details, bearing in mind that not all buildings have alarms, while
some have more than one (but only one master).  However, when
iterating through Buiding objects, a call to display
"alarm.manufacturer__name" gives me an "AttributeError: 'Building'
object has no attribute 'alarm'".  Clearly, this is because, from the
building point of view, there is no known relationship to Alarm.

How do I create such a relationship in Django, without changing the
database structure (e.g. creating an alarm field on Building, with a
one-to-many relationship to Alarm)?  (and, ideally, also being able to
filter the result so only "master" alarms show up?)

Thanks
Derek

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

Reply via email to