This question has been flagged
4 Replies
15087 Views

Hi,


I want to add product type in sale order lines but the field is on another model.

How can I get its value ?


Thanks in advance !

Avatar
Discard
Author Best Answer

I put it here in case someone needs to do it as well.

Here is the final code :

class SaleOrder(models.Model):

    _inherit = "sale.order"


class SaleOrderLine(models.Model):

    _inherit = "sale.order.line"


    product_type_id = fields.Selection(related='product_id.product_tmpl_id.type', string='Type')


I found it thanks to this link : https://www.odoo.com/fr_FR/forum/aide-1/question/odoo-10-how-to-set-a-product-category-field-to-the-sale-order-line-128344

Avatar
Discard
Best Answer

When you need to show the value of a field from a relational model to the current model you can do that by defining a related field. To do that you need in the field definition to define attribute related with value sequence of field names.

class AccountInvoiceInherited(models.Model):

    _inherit = 'account.invoice'

    company_selection = fields.Many2one('account.analytic.account', string="Company")

class AccountMoveInherited(models.Model):

    _inherit = 'account.move'

    rel_account_invoice = fields.Many2one('account.invoice', string='Related Account Invoice')

    rel_company_selection = fields.Many2one('account.analytic.account', string="Company",     related='rel_account_invoice.company_selection')
Avatar
Discard
Author

OK, so I have this :

class SaleOrder(models.Model):

_inherit = "sale.order"

class ProductTemplate(models.Model):

_inherit = 'product.template'

class SaleOrderLine(models.Model):

_inherit = "sale.order.line"

type_id = fields.Many2one("product.template", string="Type")

rel_type_id = fields.Many2one("product.template.type", string="Type", related="type_id.type")

What am I missing ?