NotImplementedError: Missing setter function for field "Cost Price" in "Product Template"

python flask api code

@product_api.route("/addproduct" , methods=['POST'])
@tryton.transaction()
def addproduct():
       ProductTemplate.create([{'name': productname , 'type': type,'consumable':consumable, 'purchasable': purchasable , 'salable':salable  , 'cost_price_method' : cost_price_method  , 'cost_price' : cost_price , 'list_price': list_price , 'default_uom': 2 }])

product.template model

cost_price = fields.Function(fields.Numeric( "Cost Price", digits=price_digits,help="The amount it costs to purchase or make the product, "  "or carry out the service."),'get_cost_price')

products = fields.One2Many( 'product.product', 'template', "Variants",help="The different variants the product comes in.")


def get_cost_price(self, name):

        if len(self.products) == 1:

            product, = self.products

            return product.cost_price

how can i use setter function in tha tryton flask api ?

The problem is that you added a cost_price functional field on template (and you are setting the cost_price on your create call) but you did not implement any setter method for the field.

There are two solutions to fix the error:

  1. Create a setter function to store the cost price on the variant
  2. Remove the cost_price key from the create call so the value is not set.

Hope this helps!

any example of creating a setter function do i need to create it in flask ?

an example woulld be greatly appreciated

by creating a setter function . do you mean to implement a function that would set variant in product.product ?

I meant implementing a function that implements the setter API

If you search for setter on the source code of tryton there are a lot of examples.