Welcome!

Share and discuss the best content and new marketing ideas, build your professional profile and become a better marketer together.

Sign up

You need to be registered to interact with the community.
This question has been flagged
1 Reply
46 Views

Hey Cyllo folks, quick question: When's the right time to use `@api.model` in my code? I'm a bit fuzzy on the best use cases. Thanks!

Avatar
Discard

Here's how you can use the '@api.model' decorator in Odoo. This approach is ideal for methods that perform actions at the model level, rather than on specific records.

@api.model This decorator is used for methods that do not depend on any specific record. It means the method can be called without needing a particular recordset and operates in the context of the model, rather than on individual records.


from odoo import models, api
​class CustomModel(models.Model):
  ​_name = 'custom.model'

  ​@api.model
  ​def my_model_method(self):
  ​# code that does not rely on a particular record
  ​return "This method can be called without records"

Avatar
Discard

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!