Odoo Help

0

Create multiple sale_order_line recursively

By
Jerome Guerriat
on 10/19/15, 4:14 PM 548 views

Hi everyone. I'm new to odoo and i need your help (again).

So i created a module in which you can link products with a m2m relation.
A specific product can have "linked" product. When buying that specific product, the "linked" products have to be added to the order.
I overrided the create method of sale_order_line to be able to generate new lines for my order for any new product. This seems to work as intended.
If one of my product is linked with another one, which is also linked to a third product, it will also work, and every item will be added correctly.

But i still have one problem. When one of my product cause lines to be created (for its linked products) AND i try to manually add a product that has already been added automaticaly, my algorithm crashes with this error:

File "**********************/parts/odoo/openerp/models.py", line 4048, in <lambda>

@api.returns('self', lambda value: value.id)

AttributeError: 'NoneType' object has no attribute 'id



And the code that provoke the crash:


@api.model
def create(self, vals):
     is_new_product = True
     for linked_product in self.env["product.product"].browse(vals["product_id"]).linked_products:
         new_order_line={}
         new_order_line["product_uos_qty"] = vals["product_uos_qty"]
         [...other assignation...]
         self.create(new_order_line)

#check if this product is already is this order. If it does, overwrite the line with new quantity
     for previous_order_line in self.env["sale.order"].browse(vals["order_id"]).order_line:
         if previous_order_line.product_id.id == vals["product_id"]:
             new_values = {"product_uos_qty":previous_order_line["product_uos_qty"]+vals["product_uos_qty"],
             "product_uom_qty":previous_order_line["product_uom_qty"]+vals["product_uom_qty"]}
             previous_order_line.write(new_values)
             is_new_product = False
             break  

    if is_new_product:
         return super(sale_order_line, self).create(vals)



The crash seems to happen somewhere in my the second FOR iteration (in which i check if the product is already in the order).

Again, the only way i succeeded to make my crash happen is:

- I add the product A which has a relation with product B.
- Product B is then automatically added to the order
- I try to add another B product to the order

Does anybody see what could be wrong?

Do everything without calling to create the individual order lines using the wirte format of the one2mamy

Axel Mendoza
on 10/20/15, 1:53 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)

Stats

Asked: 10/19/15, 4:14 PM
Seen: 548 times
Last updated: 10/20/15, 1:53 AM