我希望更新只对修改后的对象进行一次
I want the update is only done once for the modified object
我有这个方法,当我修改一条记录时,所有记录都被更改,我只想对修改后的 "update" 做,而不是对所有其他记录做
@api.multi
def _calculate_geom(self):
for record in self:
if record.cor != False:
self.env.cr.execute('SELECT ST_Transform(ST_GeomFromText(\'MULTIPOLYGON((('+str(record.cor).encode('utf-8')+')))\',4326),900913)')
record.cor2=record.env.cr.fetchone()[0]
if record.cor2 != False:
self.env.cr.execute('UPDATE '+self._name.replace('.', '_')+' set the_geom=\''+record.cor2+'\'')
我没有误解你的问题,你只需要在更新查询之后添加一个 break
语句,这样 for
循环会在第一次执行该查询后退出。
除非指定 WHERE 子句,否则 UPDATE 将影响所有行。
要仅影响预期的行,您需要添加一个“WHERE id=%d”。
此外,您不应该使用串联构建 SQL,它有风险。
类似于:
sql = "UPDATE %s SET the_geom=%s WHERE id=%d"
self.env.cr.execute(sql, self._tablename, record.cor2, record.id)
除非我遗漏了什么,如果我是你我会这样做:
record.the_geom = record.cor2
我有这个方法,当我修改一条记录时,所有记录都被更改,我只想对修改后的 "update" 做,而不是对所有其他记录做
@api.multi
def _calculate_geom(self):
for record in self:
if record.cor != False:
self.env.cr.execute('SELECT ST_Transform(ST_GeomFromText(\'MULTIPOLYGON((('+str(record.cor).encode('utf-8')+')))\',4326),900913)')
record.cor2=record.env.cr.fetchone()[0]
if record.cor2 != False:
self.env.cr.execute('UPDATE '+self._name.replace('.', '_')+' set the_geom=\''+record.cor2+'\'')
我没有误解你的问题,你只需要在更新查询之后添加一个 break
语句,这样 for
循环会在第一次执行该查询后退出。
除非指定 WHERE 子句,否则 UPDATE 将影响所有行。 要仅影响预期的行,您需要添加一个“WHERE id=%d”。 此外,您不应该使用串联构建 SQL,它有风险。
类似于:
sql = "UPDATE %s SET the_geom=%s WHERE id=%d"
self.env.cr.execute(sql, self._tablename, record.cor2, record.id)
除非我遗漏了什么,如果我是你我会这样做:
record.the_geom = record.cor2