写不工作 Odoo 8

Write not working Odoo 8

当我尝试修改 res.partner 的信息时遇到问题,但仅在这个文件中,在我的其他模块中。write 正在工作...

我知道它正在写入,它会带来信息,但它不会保存。

它之前也可以工作,但没有明显的原因它不再工作了。

有人知道可能导致这种情况的原因吗?

# -*- coding: utf-8 -*-
from openerp import models, fields, api, tools, exceptions
from openerp.exceptions import Warning
import json, urllib, time


class MAJClientsWizard(models.Model):
    _name = "maj.clients"

    @api.one
    def maj_clients(self):
        erreur = ""
        clients = self.env["res.partner"].search([['is_company', '=', True], ['customer', '=', True]])
        for client in clients:
            retour = maj_coordonnees(client)
            if retour:
                erreur += retour + ", "
        if erreur:
            raise Warning("Les détaillants qui ont une erreur dans leur code postal sont: ", erreur, "Tous les autres ont été mis à jour!")
        else:
            raise Warning("Tous les détaillants ont été mis à jour avec succès!")


def maj_coordonnees(client):
    if client.date_localization < time.strftime("%Y-%m-%d"):
        if client.zip:
            result = geo_find(client.zip)

            if result:
                client.write({
                    'partner_latitude': result[0],
                    'partner_longitude': result[1],
                    'date_localization': (time.strftime("%Y-%m-%d"))
               })
            else:
                return client.name
        else:
            return client.name


def geo_find(addr):
    url = 'https://maps.googleapis.com/maps/api/geocode/json?sensor=false&key=*****&address='
    url += urllib.quote(addr.encode('utf8'))

    try:
        result = json.load(urllib.urlopen(url))
    except Exception, e:
        return 'Network error, Cannot contact geolocation servers. Please make sure that your internet connection is up and running (%s).' + e
    if result['status'] != 'OK':
        return None

    try:
        geo = result['results'][0]['geometry']['location']
        return float(geo['lat']), float(geo['lng'])
    except (KeyError, ValueError):
        return None

显然这是行不通的。简而言之,原因是您从函数中引发错误,这使 Odoo 认为您的函数出了问题,并且实际写入数据库没有发生(发生的是回滚)。为确保数据被写入数据库,您应该在引发错误之前显式调用 commit() 函数,如下所示:

class MAJClientsWizard(models.Model):
    _name = "maj.clients"

    @api.one
    def maj_clients(self):
        erreur = ""
        clients = self.env["res.partner"].search([['is_company', '=', True], ['customer', '=', True]])
        for client in clients:
            retour = maj_coordonnees(client)
            if retour:
                erreur += retour + ", "

        self.env.cr.commit() #NOTE: commit changes to database before raising an error in order to prevent rollback

        if erreur:
            raise Warning("Les détaillants qui ont une erreur dans leur code postal sont: ", erreur, "Tous les autres ont été mis à jour!")
        else:
            raise Warning("Tous les détaillants ont été mis à jour avec succès!")

P.S.
虽然它与报告的问题无关,但我会注意到,出于性能原因,通常应使用 @api.multi 而不是 @api.one。