Search code examples
pythonodooodoo-10

How to write to database all cache in Odoo environment?


I'm doing a test where I need to imitate the onchange behavior, so:

with self.env.do_in_onchange():
    self.onefield = "blahblah"

But when one exits the with block, that data is not written to DB. I'm looking for some kind of self.env.cache.write_to_db(). Do you know of any?


Solution

  • I found the solution.

    To write a record's cache:

    self.write(self._convert_to_write(self._cache))
    

    To write all the environment's cache:

    models = dict()
    for field, cache in self.env.cache.iteritems():
        for id_, value in cache.iteritems():
            models.setdefault(field.model_name, list())
            models[field.model_name].append(id_)
    
    for name, ids in models.iteritems():
        for record in self.env[name].browse(ids):
            record.write(record._convert_to_write(record._cache))