如何解决使用django-tables2从单元格和api合并数据
class City(models.Model):
name = CharField('name',max_length=120)
ident_code = CharField("unique identification code",max_length=24)
def get_population(self):
return get_info(self.ident_code)["population"]
def get_size(self):
return get_info(self.ident_code)["size"]
和一个简单的表:
class CityTable(tables.Table):
pop = tables.Column(accessor="get_population",verbose_name="population")
size = tables.Column(accessor="get_size",verbose_name="size")
class Meta:
model = City
sequence = ("id","unique_ident","name","pop","size",)
通过ListView分配:
class CityListView(SingleTableView):
model = City
template_name = "app/cities.html"
table_class = CityTable
现在,我已经准备好一个带有函数get_info
的外部API,该函数在传递唯一的标识码时会传递有关城市的信息。几乎对每个单元都执行该功能。只需拨打get_info("ident_code")
,我就能得到一本包含更多项目的字典{"name": "cityname","population": 123446543,"size": ...}
-如何将通话减少到每行一次?有没有一种方法可以在get_context
中使用CityListView
或进行一次呼叫并将变量分配给某些键,例如:
def get_context(self):
inf = get_info(xxx) # how do I get the xx?
context = {"population": inf["population,"size": inf["size"] ...}
return context
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。