models.py
这是经理类
class ProductAttributeManager(models.Manager):
def all(self):
return super(ProductAttributeManager , self)
def type(self):
return self.all().filter(name__icontains='type')
def fabric(self):
return self.all().filter(name__icontains='fabric')
这是模型
class ProductAttribute(models.Model):
product = models.ForeignKey(Product)
name = models.CharField(max_length=100)
Value = models.CharField(max_length=100)
objects = ProductAttributeManager()
这里是我使用productattribute集来获取属性名称和值并在调用时获取错误的模板
{% for attribute in product.productattribute_set.all %}
<tr><td><strong>{{ attribute.name }}</strong></td><td>{{ attribute.Value }}</td></tr>
{% endfor %}
这里是追溯
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
164. response = response.render()
File "C:\Python27\lib\site-packages\django\template\response.py" in render
158. self.content = self.rendered_content
File "C:\Python27\lib\site-packages\django\template\response.py" in rendered_content
135. content = template.render(context, self._request)
File "C:\Python27\lib\site-packages\django\template\backends\django.py" in render
74. return self.template.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
209. return self._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in _render
201. return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
903. bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
135. return compiled_parent._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in _render
201. return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
903. bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
65. result = block.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
903. bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
161. values = list(values)
Exception Type: TypeError at /products/3/
Exception Value: 'super' object is not iterable
我第一次遇到这个问题。 为什么超级对象不可迭代?
答案 0 :(得分:1)
您的all()
方法应该返回一个查询集。例如,你可以做
class ProductAttributeManager(models.Manager):
def all(self):
return super(ProductAttributeManager , self).all()
但是,上述方法实际上并未改变行为,因此您只需删除all()
方法即可。