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

I have created a price list tab in product.template in which I have created a table product.template.line with the field x_name. How can I now display this field as a selection field in the purchase.order.line table, which only displays the number of the selected product?


The code works with a char field:


from odoo import api, fields, models


class PurchaseOrderLine(models.Model):

    _inherit = 'purchase.order.line'


    x_studio_pc = fields.Char(

        string="PC",

        compute='_compute_pcs',

        store=True

    )


    @api.depends('product_id', 'product_id.product_tmpl_id')

    def _compute_pcs(self):

        for line in self:

            names = []

            tmpl = line.product_id.product_tmpl_id

            if tmpl and hasattr(tmpl, 'x_pc_liste'):

                for pc_line in tmpl.x_pc_liste:

                    if pc_line.x_name:

                        names.append(pc_line.x_name)

            line.x_studio_pc = ", ".join(names)


Thank you very much

形象
丢弃

I'm not sure what you try to achieve. A selection field has a rather fixed set of options. It looks more like you want to have a Many2one on your Product Template and a Many2many on your PO line.

编写者 最佳答案

My question is how can I load the data update for the selected product in the list when I click or select the Many2one x_studio_pc field. Currently all PCs are loaded for each product.


from odoo import api, fields, models, _

from odoo.exceptions import UserError


class PurchaseOrderLine(models.Model):

    _inherit = 'purchase.order.line'


    x_studio_pc = fields.Many2one(

        'product.template.line',

        string='PC'

    )


    @api.onchange('product_id')

    def _onchange_product_id_set_pc_domain(self):

        tmpl = self.product_id.product_tmpl_id

        raise UserError(_(self.product_id.name))

        if tmpl and hasattr(tmpl, 'x_pc_liste') and tmpl.x_pc_liste:            

            pc_ids = tmpl.x_pc_liste.ids

            return {

                'domain': {

                    'x_studio_pc': [('id', 'in', pc_ids)]

                }

            }

        else:

            return {

                'domain': {

                    'x_studio_pc': []

                }

            }

形象
丢弃
最佳答案

Hi,

Please refer to the code below:

Python


Model: Product Template

class ProductTemplate(models.Model):
_inherit = 'product.template'

x_pc_liste = fields.One2many('product.template.line', 'product_tmpl_id', string='PC List')


Model: Product Template Line

class ProductTemplateLine(models.Model):
_name = 'product.template.line'
_description = 'Product Template Line'

x_name = fields.Char(string='PC Name')
product_tmpl_id = fields.Many2one('product.template', string='Product Template')

Model: Purchase Order Line


class PurchaseOrderLine(models.Model):
"""
Inherits Purchase Order Line to add a custom field `x_studio_pc` that allows
the user to select a specific PC entry (from `product.template.line`) associated
with the selected product's template.

The field is a Many2one relation and uses a domain to restrict selectable
PC lines to only those linked to the current product's template.
"""
_inherit = 'purchase.order.line'

x_studio_pc = fields.Many2one(
'product.template.line',
string='PC',
domain="[('product_tmpl_id', '=', product_id.product_tmpl_id)]"
)


Hope it helps.

形象
丢弃
最佳答案

Hii,


here is update code  of custom module


models/product_template.py

from odoo import models, fields



class ProductTemplate(models.Model):

    _inherit = 'product.template'


    x_pc_liste = fields.One2many('product.template.line', 'product_tmpl_id', string="PC List")


models/purchase_order_line.py


from odoo import models, fields



class PurchaseOrderLine(models.Model):

    _inherit = 'purchase.order.line'


    x_pc_name = fields.Many2one(

        'product.template.line',

        string='PC Name',

        domain="[('product_tmpl_id', '=', product_id.product_tmpl_id)]"

    )


Add this to your purchase order line form (usually in purchase_order_views.xml):

<odoo>

    <record id="view_order_form_inherit_pc_name" model="ir.ui.view">

        <field name="name">purchase.order.form.pc.name</field>

        <field name="model">purchase.order</field>

        <field name="inherit_id" ref="purchase.purchase_order_form"/>

        <field name="arch" type="xml">

            <xpath expr="//field[@name='order_line']/form//field[@name='price_unit']" position="after">

                <field name="x_pc_name"/>

            </xpath>

        </field>

    </record>

</odoo>


i hope it is help full to solve your problem


形象
丢弃
相关帖文 回复 查看 活动
1
5月 25
729
2
5月 25
1017
0
5月 25
669
1
4月 25
724
1
4月 25
840