【问题标题】:django-tables2 ignores ManyToManyFielddjango-tables2 忽略 ManyToManyField
【发布时间】: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


    【解决方案1】:

    这样做:

    class ItemTable(tables.Table):
        class Meta:
            model = Item
            attrs = {"class": "paleblue"}
            fields = ("uuid", "name", "brand", "categories")
    
        categories = tables.Column()
    
        def render_categories(self, value):
            if value is not None:
                return ', '.join([category.name for category in value.all()])
            return '-'
    

    【讨论】:

    • 我发现 value = None 是 django-tables2 中的一个错误的结果(看我上面的回答),它忽略了模型中的 ManyToManyField。因此,如果我按照您所写的去做 - 结果总是会得到 '-'。解决方法是修复django-tables2中的bug
    【解决方案2】:

    当与 django 1.7 一起使用时,看起来这是 django-tables2 中的错误: https://github.com/bradleyayers/django-tables2/issues/211

    【讨论】:

      【解决方案3】:

      看起来它尚未合并。作为一种解决方法,您可以使用记录进行渲染并以相同的方式进行迭代。

      即。

       def render_categories(self, record):
              if record.category is not None:
                  return ', '.join([category.name for category in record.category.all()])
              return '-'
      

      【讨论】:

      • 糟糕。您需要使用一个新字段,因为它看起来像 django-tables2 基于该字段返回。因此,只需获取一个新列或其他内容,以便获得完整记录。
      • 嗨@Craig,欢迎来到 StackOverflow。你可以edit你的问题来掩盖你的耻辱。如果已经过了五分钟,它将被记录为编辑,但最好是编辑而不是作为评论发布更正。 :)
      【解决方案4】:

      也许不是更清洁的选择,但这对我有用:

      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)
      
         @property
         def categories_str(self):
             return ', '.join([category.name for category in value.all()])
      
      
      class ItemTable(tables.Table):
          categories = tables.Column(accessor='categories_str')
      
          class Meta:
              model = Item
              attrs = {"class": "paleblue"}
              fields = ("uuid", "name", "brand", "categories")
      

      【讨论】:

        【解决方案5】:

        这样做:

        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, record):
                if record.categories.all():
                    return ', '.join([category.name for category in record.categories.all()])
                return '-'
        

        注意empty_values=()写在tables.Column()中很重要

        【讨论】:

          猜你喜欢
          • 2020-01-24
          • 1970-01-01
          • 2011-05-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-05
          • 1970-01-01
          相关资源
          最近更新 更多