我有2个型号。
class Unit(models.Model):
title = models.CharField(max_length=60)
def __unicode__(self):
return self.title
class UniDoc(models.Model):
title = models.CharField(max_length=100, blank=True, null=True)
units = models.ManyToManyField(Unit, blank=True)
file = models.FileField(upload_to="uploads/")
author = models.ForeignKey(User, null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.file.name
如何在模板中的链接ti单元上获取unidoc列表?
答案 0 :(得分:0)
Django提供reverse relationships:
u = Unit.objects.get(pk=1)
u.unidoc_set.all()
因此,在您的模板中,如果您已从上方传递u
:
{% for i in u.unidoc_set.all %}
{{ i.title }}
{% endfor %}