4
我有一位居民,並且似乎無法獲得居民所屬的SSA集。我試過res.ssa_set.all()
.ssas_set.all()
和.ssa_resident_set.all()
。似乎無法管理它。通過另一個表進行反向m2m查找的語法是什麼?Django模型API通過中間表進行多對多關係反向查找
編輯:我得到'查詢集作爲沒有屬性'的錯誤。呃?
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)
這個答案並沒有給我太多的清晰度。你能提供一個工作的例子嗎? – mlissner 2011-04-06 03:10:26