Mat*_*att 2 python django django-tables2 web
def calculateTotalVists(days):
total = 0
for i in days:
total += i
return total
class ClientTable(tables.Table):
class Meta:
model = Client
fields = ('id', 'name', 'phone')
attrs = {'class': 'table table-striped table-bordered', 'id': 'clients'}
Run Code Online (Sandbox Code Playgroud)
客户端模型具有"days"字段,该字段是列表.我想创建一个自定义列,在天数传递到calculateTotalVists()之后显示结果.但是,我不是如何使用具有函数的访问器.
你不能使用随机函数,并期望tables2神奇地传递从表记录中获取的适当参数.只需将其作为客户端模型上的方法(或属性):
# models.py
class Client(models.Model):
# ...
def total_visits(self):
return sum(self.days) # or whatever self.days-based calculation
# tables.py
from django_tables2.utils import A
class ClientTable(tables.Table):
total_visits = Column(
accessor=A('total_visits'),
# ...
)
class Meta:
model = Client
fields = ('id', 'name', 'phone')
sequence = ('id', 'name', 'phone', 'total_visits')
# ...
Run Code Online (Sandbox Code Playgroud)