问题描述
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 (将#修改为@)