我試圖編寫一個函數來存儲幾個字段的連接sting,包括一些存儲在M2M關係的關係表中的函數。 下面是代碼:django查找到M2M關係中的導航表
class substance(models.Model):
name = models.Charfield()
...
class rel_med_subs(models.Model):
id_med = models.ForeignKey(medication, related_name='relation_med')
id_subs = models.ForeignKey(substance, related_name='relation_subs')
order = models.IntegerField()
strength = models.FloatField(null=True, blank=True)
strength_unit = models.ForeignKey(units)
class medication(models.Model):
name = models.Charfield()
substance = models.ManyToManyField(substance, through='rel_med_subs')
autogenerated = models.CharField(blank=False, default='')
...
def save(self, force_insert=False, force_update=False):
self.autogenerated = u'%s' % (' '.join[(u'%s %s'(s.name, s.strength) for s in self.rel_med_subs.order_by('order')])
self(medication,self).save(force_insert,force_update
當我只要求姓名,功能工作正常,但如果我嘗試從關係錶帶來的屬性,Django的抱怨強度不是物質模型的一部分。
如何從關係中獲取屬性到自動生成的字段中?
謝謝!