【发布时间】:2014-10-03 13:21:07
【问题描述】:
我有这些模型:
class Category(models.Model):
name = models.CharField(max_length=10)
uuid = models.CharField(max_length=36)
class Item(models.Model):
name = models.CharField(max_length=255)
uuid = models.CharField(max_length=36)
categories = models.ManyToManyField(Category, null=True)
brand = models.ForeignKey(Brand)
我正在尝试使用 django-tables2 在表格中显示 Item 模型,如下所示:
class ItemTable(tables.Table):
class Meta:
model = Item
attrs = {"class": "paleblue"}
fields = ("uuid", "name", "brand", "categories")
categories = tables.Column(empty_values=())
def render_categories(self, value):
return ', '.join([category.name for category in value.all()])
它工作正常,除了 Table 忽略 categories 字段和
value 参数等于 None,我收到错误 'NoneType' object has no attribute 'all'
我做错了什么?谢谢。
【问题讨论】:
标签: python django many-to-many django-tables2