【问题标题】:How to add space and $ in front of column in django-tables2如何在 django-tables2 的列前添加空格和 $
【发布时间】:2019-03-09 13:54:04
【问题描述】:

我的 django 表中有两个美元列,但是,我不喜欢间距以及它就在前一列旁边的事实。我还想添加一个美元符号。我怎样才能添加这个?

现在看起来像这样:

Price1Price2
345.09 154.35

我希望它看起来像这样:

Price1    Price2
$345.09   $154.35

我的 django 表是基本的:

class PriceTable(tables.Table):

    class Meta:
        model = Price
        template_name = 'django_tables2/bootstrap.html'
        sequence = ('service', 'price1', 'price2')
        exclude = ("comments")
        attrs = {"class": "paleblue"}

这些列在 models.py 中是这样定义的:

price1 = models.DecimalField(max_digits=6, decimal_places=2, blank=True)
price2 = models.DecimalField(max_digits=6, decimal_places=2, blank=True)

还有一个问题,是否有 django-tables2 表格描述功能或其他可以在表格下方轻松添加表格信息的功能?

此代码基于 Dan 的出色回答!他用这个代码让它处理同一个表中的数据:

class Price1Column(tables.Column):
  def render(self, record):
      return ' ${}'.format(record.price1)

现在我想要另一个表中的一列 - 服务,但这不起作用:

class Price2Column(tables.Column):
  def render(self, record):
      return ' ${}'.format(record.tables.Column(accessor='service.price2'))
  class Meta:
      model = Price

这是服务的模型,它通过代码连接到价格。 tables.Column(accessor=) 行在 PriceTable() 类中工作。

class Service(models.Model):
    serviceid = models.UUIDField(default=uuid.uuid4, help_text='Unique ID for this particular service in database')
    desc_us = models.TextField(blank=True, primary_key = True)
    code = models.IntegerField(default= 10000)
    price2 = models.DecimalField(max_digits=8, decimal_places=2, blank=True)

    class Meta:
        ordering = ['serviceid']

    def __str__(self):
        """String for representing the Model object."""
        return self.desc_us

【问题讨论】:

    标签: django django-models django-tables2


    【解决方案1】:

    根据docs,我假设你是这样做的:

    class Price1Column(tables.Column):
    
        def render(self, record):
            return '${}'.format(record.price1)
    
    class Price2Column(tables.Column):
    
        def render(self, record):
            return '${}'.format(record.price2)
    
    class PriceTable(tables.Table):
        # ...
        price1 = Price1Column()
        price2 = Price2Column()
    

    【讨论】:

    • @JR 最后两行应该放在PriceTable 类中
    • 我不确定您是否知道这一点,但我如何从另一个模型中添加列。我在上面加了一点代码。
    • @JR 添加在哪里?
    • 抱歉,刚刚添加。
    • @JR 也为该字段添加模型
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 2017-07-23
    • 2018-02-07
    相关资源
    最近更新 更多