Skip to content

Instantly share code, notes, and snippets.

@sehrishnaz
Last active April 29, 2022 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sehrishnaz/7b31f6f5c33f125b02dd95ae993d472b to your computer and use it in GitHub Desktop.
Save sehrishnaz/7b31f6f5c33f125b02dd95ae993d472b to your computer and use it in GitHub Desktop.
How to use .copy() and .copy_data() function in Odoo15
class your_model(models.Model):
_name = "your.model"
def copy_data(self, default=None):
defaults = super().copy_data(default=default)
if 'from_duplicate' in self.env.context and self.env.context.get('from_duplicate'):
return [{k: v for k, v in default.items()} for default in defaults]
else:
return [{k: v for k, v in default.items() if k in self.SELF_READABLE_FIELDS} for default in defaults]
def btn_duplicate(self):
self.ensure_one()
# magic code
duplicate_rec = self.copy()
# if the above code not work than use below code
values = self.with_context({'from_duplicate':True}).copy_data()[0]
# you can set the default values
values['field_1'] = self.xyz
values['field_2'] = '/'
self.create(values)
@sehrishnaz
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment