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
49 Views

When and why would I use `@api.returns` in my code? Trying to understand its purpose better. Thanks!

Avatar
Discard

Here's how you can use the `@api.returns` decorator to handle different output styles for your methods. This example demonstrates it's use with a custom model's 'copy' method. The '@api.returns' decorator for methods that return instances of a specified model.

class CustomModel(models.Model):
  ​_name = 'custom.model'


  ​@api.returns('self')
  ​def copy(self, default=None):
  ​# Custom logic for copying a record
  ​return super(CustomModel, self).copy(default)

It takes three parameters:
model - which is the name of the model or 'self' for the current model;
downgrade - a function that converts record-style output to traditional output; 
upgrade - a function that converts traditional output to record-style output. 

The decorator adjusts the method output based on the call style, allowing it to return an ID, a list of IDs, or a recordset. 

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!