我的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
这是服务模型,它通过代码连接到Price。 tables.Column(acessor =)行在Class 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