我有两个模型:
class Concordance(models.Model):
name = models.CharField(max_length=64)
tunes = models.ManyToManyField(MsTune, related_name="concordances")
def __str__(self):
return self.name
class MsTune(models.Model):
name = models.CharField(max_length=255, db_index=True) # title (source)
[etc...]
@property
def concordances(self):
for concordance in self.concordances.all:
for t in concordance.tunes.all:
[stuff]
return '-' + t.name
然后我想在我的模板中显示该属性:
{{ tune.concordances|safe }}
我的问题是 self__concordances.all
始终显示为 none
,即使有数据。我错过了什么?这是我在模板中得到的输出:
bassculture.Concordance.None
为了记录,此代码直接在我的模板中有效:
{% for concordance in tune.concordances.all %}
{% for t in concordance.tunes.all %}
- {{ t.name }}
{% endfor %}
{% endfor %}
我想要的是从模型中的@property 中获取数据,以查看是否可以加快检索过程。
答案 0 :(得分:1)
您正在用自己的属性覆盖自动 concordances
关系字段。更改您的资源名称以解决此问题。
另外,不要忘记在更改名称后调用 all
方法。我假设您已删除该调用,因为它以异常结束。