Django-tables上的自定义列2混合了多个外国人



我试图在django-tables上制作一个自定义列,该函数返回从3个可能的外键中返回的字段(只有一个可以不为空(。这是我所做的:

models.py

class Sitiocancelado(models.Model):
    x = models.OneToOneField(X, on_delete=models.CASCADE, null=True,blank=True)
    y = models.OneToOneField(Y, on_delete=models.CASCADE, null=True, blank=True)
    z = models.OneToOneField(Z, on_delete=models.CASCADE, null=True, blank=True)
    fecha_de_cancelacion = models.DateField(null=True, blank=True)
    comentarios = models.TextField(max_length=200, null=True, blank=True)
    slug = models.SlugField(max_length=100, editable=False)
    def __str__(self):
        return self.z.y.x.nombre_del_sitio

tables.py

class SitiocanceladoTable(tables.Table):
    def columna_combinada(table):
        if x == None:
            if y == None:
                return z
            else:
                return y
        else:
            return x
    Sitio = tables.Column(columna_combinada)
    class Meta:
        model = Sitiocancelado
        attrs = {
            'class': 'table table-striped table-bordered', 
            'id': 'example', 
            'width' : '100%', 
        }
        fields = ('Sitio',)
        exclude = ('id', 'slug', 'sitioproyecto', 'sitiocontratado', 'sitiooperando',)

这有意义吗?

这并不是自定义列的工作方式。您基本上有两个选择:

render_<columnname>

这看起来有点像您的示例。用名称render_<column name>在表类上定义列和方法。无需将该名称提供给您定义的列。您的示例看起来像这样:

class SitiocanceladoTable(tables.Table):
    Sitio = tables.Column(empty_values=())
    class Meta:
        model = Sitiocancelado
    def render_Sitio(self, record):
        if record.x == None:
            if record.y == None:
                return record.z
            else:
                return record.y
        else:
            return record.x

此方法适合一次性。它没有太多的额外代码,并且代码与表的其余定义非常接近。

请注意,我添加了emtpy_values=()。即使Django-tables 2认为您的列是空的,这将确保您自定义所使用的渲染函数的实现。自定义列的责任呈现适当的默认值。

子分类Column

如果您需要在多个表中或在同一表中多次使用此列的功能,则可能对子类Column或更具体的Column实现都更加清洁。

您的示例可能看起来像这样:

class CombinedFKColumn(tables.Column):
    def render(self, record):
        return getattr(record, 'x', getattr(record, 'y', getattr(record, 'z', self.default)))
class SitiocanceladoTable(tables.Table):
    Sitio = tables.CombinedFKColumn(empty_values=())
    class Meta:
        model = Sitiocancelado