Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
385 Vistas

Hello everyone,


At the moment I can see all the PCs entered in my list, regardless of which customer is selected in the column. However, I would like the PCs to be loaded and displayed only for the selected customers when I click on the x_studio_pc field. How do I do this? Do I have to use OWL? If so, how do I do this? I have no experience with OWL so far.


class ProductTemplate(models.Model):

    _inherit = 'product.template'


    x_studio_pc_liste = fields.One2many('product.template.line', 'x_product_template_id', string='PC Liste')


class ProductTemplateLine(models.Model):

    _name = 'product.template.line'

    _description = 'Product Template Line Modul'

    _order = 'x_product_template_id, x_studio_sequence, id'

    _rec_name = 'x_name'


    x_product_template_id = fields.Many2one('product.template', string='X Product Template', ondelete='cascade')


    x_studio_info = fields.Char(string='Info', default='Please enter the PC in Name', required=True)


    x_studio_sequence = fields.Integer(string='Sequence', default=10)


    x_name = fields.Char(string='Description', required=True)


    x_studio_distribution = fields.Selection(

        [

            ('5575', 'Test1'),

            ('5581', 'Test2'),

            ('5559', 'Test3'),

        ],

        string='Distribution',

        required=True

    )


    x_studio_note = fields.Text(string='Note')


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

       

        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': []

                }

            }

Avatar
Descartar
Mejor respuesta

Hi,

Please refer to the code below:


Model: Product template line

- Added new field for customer.

from odoo import models, fields

class ProductTemplateLine(models.Model):
_name = 'product.template.line'
_description = 'Product Template Line Modul'
_order = 'x_product_template_id, x_studio_sequence, id'
_rec_name = 'x_name'

x_product_template_id = fields.Many2one('product.template', string='Product Template', ondelete='cascade')
partner_id = fields.Many2one('res.partner', string='Customer') #ADD THIS LINE
x_studio_info = fields.Char(string='Info', default='Please enter the PC in Name', required=True)
x_studio_sequence = fields.Integer(string='Sequence', default=10)
x_name = fields.Char(string='Description', required=True)
x_studio_distribution = fields.Selection([
('5575', 'Test1'),
('5581', 'Test2'),
('5559', 'Test3'),
], string='Distribution', required=True)
x_studio_note = fields.Text(string='Note')

Model: purchase order line

from odoo import models, fields, api

class PurchaseOrderLine(models.Model):
_inherit = 'purchase.order.line'

x_studio_pc = fields.Many2one('product.template.line', string='PC')

@api.onchange('product_id', 'order_id.partner_id')
def _onchange_product_or_partner(self):
"""
Dynamically filters the available options for the 'x_studio_pc' field
based on the selected product and customer.

Only shows PC entries (product.template.line records) that are linked
to both the selected product template and the selected customer (partner).
If either is not selected, the PC dropdown is emptied.
"""
if self.product_id and self.order_id.partner_id:
tmpl = self.product_id.product_tmpl_id
partner = self.order_id.partner_id
pc_ids = self.env['product.template.line'].search([
('x_product_template_id', '=', tmpl.id),
('partner_id', '=', partner.id)
]).ids
return {'domain': {'x_studio_pc': [('id', 'in', pc_ids)]}}
else:
return {'domain': {'x_studio_pc': []}}

Hope it helps.

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
2
jul 25
356
2
feb 25
2433
0
mar 15
3739
3
ago 25
487
1
ago 25
168