我想通过模板标签传递对象的名称,但出现此错误:
'str' object has no attribute 'mymodelone_set'
我的模型:
class mymodelone(models.Model):
name = models.CharField(max_length=50)
show = models.ForeignKey('mymodeltwo')
def __unicode__(self):
return self.title
class mymodeltwo(models.Model):
title = models.CharField(max_length=50)
description = models.TextField(blank=True)
def __unicode__(self):
return self.name
我的模板标签如下所示:
在我的 base.html 中:
{% load my_tags %}{% my_func obj%}
在我的 model_tag.py
register = template.Library()
@register.inclusion_tag('myapp/widget.html')
def my_func(obj):
param1 = obj.mymodelone_set.all()
return {}
我究竟做错了什么?