views:

1844

answers:

1

I have a Resident and can not seem to get the set of SSA's the resident belongs to. I've tried res.ssa_set.all() .ssas_set.all() and .ssa_resident_set.all(). Can't seem to manage it. What's the syntax for a reverse m2m lookup through another table?

EDIT: I'm getting an 'QuerySet as no attribute' error. Erm?

class SSA(models.Model):
    name = models.CharField(max_length=100)
    cost_center = models.IntegerField(max_length=4)
    street_num = models.CharField(max_length=9)
    street_name = models.CharField(max_length=40)
    suburb = models.CharField(max_length=40)
    post_code = models.IntegerField(max_length=4, blank=True, null=True)

    def __unicode__(self):
        return self.name

class Resident(models.Model):
    cris_id = models.CharField(max_length=10, primary_key=True)
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=20)
    ssas = models.ManyToManyField('SSA', through='SSA_Resident', verbose_name="SSAs")

    def __unicode__(self):
        return self._get_full_name()

    def _get_full_name(self):
        return u"%s %s" %(self.first_name, self.last_name)

    full_name = property(_get_full_name)

class SSA_Resident(models.Model):
    id = models.AutoField(primary_key=True)
    resident = models.ForeignKey('Resident')
    ssa = models.ForeignKey('SSA', verbose_name="SSA")
    active = models.BooleanField(default=True)
    def __unicode__(self):
        return u"%s - %s" %(self.resident.full_name, self.ssa.name)
A: 

I was trying to evaluate a query set object, not the object itself. Executing a get on the query set and then a lookup of the relation set worked fine. I'm changing to community wiki and leaving this here just incase someone else is as stupid as I was.

Josh Smeaton