跳至内容
菜单
此问题已终结
2 回复
1989 查看

im trying to get the id of the current model that i edit or created but i cant get it
below is my code

def _get_basic_product_information_purchase(self, product_or_template, pricelist, combination, **kwargs):
        basic_information = dict(
            **product_or_template.read(['description_purchase', 'display_name'])[0]
        )
       
        if not product_or_template.is_product_variant:
            basic_information['id'] = False
            combination_name = combination._get_combination_name()
            if combination_name:
                basic_information.update(
                    display_name=f"{basic_information['display_name']} ({combination_name})"
                )
       
        price = product_or_template.standard_price

        purchase_order_id = int(kwargs.get('id', 0))
   
        if purchase_order_id:
            purchase_order = request.env['purchase.order'].browse(purchase_order_id)
           
            if purchase_order:
                seller = product_or_template.seller_ids.filtered(lambda s: purchase_order.partner_id.id)
                if seller:
                    price = seller[0].price

        return dict(
            **basic_information,
            price=price
        )
形象
丢弃
最佳答案

Hello Muhammad Ihza Putra Handayani, 


    The issue seems to stem from the way you're handling the ID of the current model (product_or_template). 

    To ensure that you're getting the correct ID of the product or template, you should add a mechanism to 

    extract it directly from the product_or_template object, especially when product_or_template.is_product_variant is True.


    Here's how you can modify your function to correctly extract and use the ID of the product_or_template object.

// Code in comment //

    Hope this helps.

    

    If you need any help in customization feel free to contact us.

        

Thanks & Regards,

Email:  odoo@aktivsoftware.com           

Skype: kalpeshmaheshwari 

形象
丢弃

Code :

def _get_basic_product_information_purchase(self, product_or_template, pricelist, combination, **kwargs):
# Initialize basic information dictionary
basic_information = dict(
**product_or_template.read(['description_purchase', 'display_name'])[0]
)

# Add the product_or_template ID to the basic_information dictionary
basic_information['id'] = product_or_template.id

# If the product_or_template is not a product variant
if not product_or_template.is_product_variant:
basic_information['id'] = False # Reset ID to False
combination_name = combination._get_combination_name()
if combination_name:
basic_information.update(
display_name=f"{basic_information['display_name']} ({combination_name})"
)

# Get the standard price of the product_or_template
price = product_or_template.standard_price

# Get the purchase order ID from kwargs
purchase_order_id = int(kwargs.get('id', 0))

if purchase_order_id:
# Browse the purchase order using the ID
purchase_order = request.env['purchase.order'].browse(purchase_order_id)

if purchase_order:
# Find the seller linked to the purchase order partner
seller = product_or_template.seller_ids.filtered(lambda s: s.name.id == purchase_order.partner_id.id)
if seller:
# Use the seller's price if available
price = seller[0].price

# Return the basic information along with the price
return dict(
**basic_information,
price=price
)

编写者

thank you very much for your helps but it seems that it not reach what i desire. actually the id of what i am going to get is from purchase.order and if i try your below code i dont even get any id
but if try straight search of id in purchase order it worked well
price = product_or_template.standard_price
purchase_order = request.env['purchase.order'].search([('id', '=', self.id)])

for rec in purchase_order:
seller = product_or_template.seller_ids.filtered(lambda x: x.partner_id.id == rec.partner_id.id)
if seller:
price = seller[0].price

any idea? im stuck in this

相关帖文 回复 查看 活动
3
6月 24
3813
0
7月 19
4797
2
5月 15
20533
2
6月 25
1094
3
4月 23
11576