Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
149 Zobrazení

Hi all,

I noticed something a bit curious after shipping a new feature on a customized Odoo 18 instance on Odoo.sh. We built a custom integration with a service, and part of our implementation involved overriding the create​ and write​ methods in the res.partner​ model to make calls to the service (see the example code below).

  def create(self, vals):

    '''

      Override account creation so that a brivo user is

      created along with this user.

    '''

    res = super().create(vals)

    res._create_brivo_user()

    return res


  def write(self, vals):

    '''

      Override account editing so that the brivo user

      is edited along with this user.

    '''

    res = super().write(vals)

    # Update or create the brivo user on write if the user is active

    if not self.env.context.get('skip_brivo_call_on_write', False) and self.active:  

      if self.brivo_id:

        brivo_update_user(self.env, self)

      else:

        self._create_brivo_user()

    # If the user's barcode was updated,

    # update the Brivo user's credential

    if not self.env.context.get('skip_brivo_call_on_write', False) and vals.get('barcode') and self:

      self._update_user_barcode_credential()

    return res

So, when we call create​, we create a corresponding user on the service (Brivo). We then use the context to avoid making extra calls in write​ because we understand that there's an automatic call to it that's made after the call to create​.

This flow worked perfectly fine on a local (neutralized) copy of our database. However, when we tested it in a production database, we were unable to create new contacts on Odoo. Inspecting the logs, I noticed that there were extra, unexpected calls being made to the Brivo service that were causing creation to fail. After a bit of digging, I realized that the write​ method was being called before the new contact record was being written to the database. We fixed the issue by removing our create​ override.

I expected the flow to be like this: create​, then write to database, then write​. This seemed to be how it worked in the local copy of the database. That doesn't seem to be the case in the production copy.

What are the differences in the create/write lifecycle between a production and a neutralized database, if any?

Avatar
Zrušit

Neutralized databases are just "cut off" from integrations such as bank feeds, shipping, email - things that allow them to "contact the outside world".

Think of CREATE as a database SQL INSERT and WRITE as a database SQL UPDATE.

WRITE is not part of CREATE nor is it called before or after it - it is a separate call. Something else is going on.

What may be happening is that your CREATE call is providing data for a field that is used as a dependency for a class method that provides the value for another field, so WRITE is called after your call because of this.

Creating a new res.partner with a value for parent_id will trigger a write to compute the value of commercial_partner_id.