【发布时间】: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