【发布时间】:2021-07-22 20:33:49
【问题描述】:
以下面的数据为例,其中prop 是我要更新的查询集,results 是信息字典。此 dict 具有作为此查询集中属性的键,并且还具有不是此 QuerySet 中的属性的键。如果它的属性匹配,我只想更新这个 QuerySet...
prop = Property.objects.get(pk=14)
results = {'price': '525000', 'code': '55285', 'estimate': '1500'}
我知道这不起作用,因为它需要一个字面上名为“apicall”的属性,但这是我希望实现的逻辑:
for apicall in results:
if hasattr(prop,apicall):
prop.apicall = results[apicall]
prop.save()
如果查询集中存在prop.apicall,我如何避免直接调用prop.price?
【问题讨论】:
标签: python django django-queryset