There's more...

When extending write(), note that, before calling the super() implementation of write(), self is still unmodified. You can use this to compare the current values of the fields to the ones in the values dictionary.

In this recipe, we chose to raise an exception, but we could have also chosen to remove the offending field from the values dictionary and silently skipped updating that field in the record:

 @api.multi 
 def write(self, values): 
     if not self.user_has_groups( 'my_library.group_librarian'): 
         if 'manager_remarks' in values: 
             del values['manager_remarks'] 
     return super(LibraryBook, self).write(values) 

After calling super().write(), if you want to perform additional actions, you have to be wary of anything that can cause another call to write(), or you will create an infinite recursion loop. The workaround is to put a marker in the context that will be checked to break the recursion:

class MyModel(models.Model): 
    @api.multi 
    def write(self, values): 
        sup = super(MyModel, self).write(values) 
        if self.env.context.get('MyModelLoopBreaker'): 
            return 
        self = self.with_context(MyModelLoopBreaker=True) 
        self.compute_things() # can cause calls to writes
return sup
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset