如何遍历 Django 模板中的通用关系?
我想在 Django 模板中遍历通用关系,类似于遍历 FK 关系的方式。
Models.py
class Company(models.Model):
name = models.CharField(blank=True, max_length=100)
notes = models.TextField(blank=True)
class Address(models.Model):
address = models.TextField(max_length=200)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
这似乎在我的模板中不起作用:
{{ company.address_set.all }}
感谢任何帮助。
I would like to traverse generic relationships in my Django template, similar to how you can traverse FK relationships.
Models.py
class Company(models.Model):
name = models.CharField(blank=True, max_length=100)
notes = models.TextField(blank=True)
class Address(models.Model):
address = models.TextField(max_length=200)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
This does not seem to work in my template:
{{ company.address_set.all }}
Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的公司模型不知道地址,您可以尝试以下操作:
在您的模板中,您可以执行以下操作:
希望这会有所帮助。
Your Company model doesnt know about the adresses, you could try this:
In your template you can do something like this :
Hope this helps.