Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
6 ตอบกลับ
9817 มุมมอง

I want to do some actions for every new created product.

In order to do that I need the product_id of the freshly new generated product.

I know that I can extend the create method of product.template as in:

class AdditionalActionsOnProductCreation ( models.Model )

    _inherit = 'product.template'

    @api.model
    def create(self, vals):
        ....
    # Creates the product using standard create metho
    new_product_template = super(AdditionalActionsOnProductCreation, self).create(vals

# Try to add the product_id and assign to ean13 field (just to test/see it easily)
    new_product_template.update({'ean13': new_product_template.product_id})
    
    return new_product_template                 

But product_id is not there, where should i retrieve it?

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

You did as Empiro suggested in this answer, I think you have to get a correct result. Why don't you inherit the 'product.product' class. Try and see

อวตาร
ละทิ้ง
ผู้เขียน

I have tried both new_product_template.product_id and new_product_product.product_id extending respectively product.template and product.product without success. " print new_product.product_id AttributeError: 'product.product' object has no attribute 'product_id'"

Test with new_product.product_tmpl_id

คำตอบที่ดีที่สุด

Hi EM,

Inspite of inheriting product.template, why don't you inherit product.product.

As with the creation of every product.template a product.product record is created......so you can easily get this by overriding create method of product.product.

and one more thing , we don't have product_id in product.template, but product_templ_id(product.template) in product.product.
Hope it will help you  

อวตาร
ละทิ้ง

"product_tmpl_id" not "product_templ_id"

คำตอบที่ดีที่สุด

My suggestion, you create new base.action.rule and fire it when record is created.

อวตาร
ละทิ้ง
ผู้เขียน

Could you please elaborate? Any example on how to create one? Thanks

For example, in module crm, see crm_action_rule_demo.xml

Related Posts ตอบกลับ มุมมอง กิจกรรม
1
ก.พ. 23
8165
1
พ.ค. 19
3772
3
พ.ค. 19
10725
3
มี.ค. 17
14139
Module development แก้ไขแล้ว
1
ต.ค. 15
3786