Skip to Content
Menu
This question has been flagged
1 Reply
1743 Views

helo!

I' use the sale module . 

in the sale.order.line i want to have 2 new fields so i create a new model (in my custom module) like:

class cave_vente(models.Model):
    _name = 'cave.vente'
    _description = "classe vente personnalise"
    _inherits = 'sale.order.line'

    @api.model 
    def _calcul_qt_vendu(self):
​        qty = 0
        for rec in self:
            qty= rec.qte_sortie - rec.qte_retournee
            rec.update({'product_uom_qty': qty})


     qte_sortie = fields.Integer('Quantité chargée')
     qte_retournee = fields.Integer('Quantité retournée') 
     product_uom_qty = fields.Integer('Quantité commandée', compute='_calcul_qt_vendu', store=True, readonly=True)

##########

maybe i can vreate new view based on cave.vente model - but in this new view i'll have to call again all the field in sale.order.form (and it not sure i undertand each of them) 

How can i use this new fields (qte_sortie and qte_retournee ) in sale.order.form (the view of sale.order)?

Avatar
Discard

YOU can inherit directly in sale.order.line. is that applicable? without naming a new model. thus you can inherit sale order view and add these fields in it.

Best Answer


If you want to just add 2 new fields to the existing sale order line model, you do not have to use  the line 

_name = 'cave.vente'

this line would treat the newly declared class as a new table in Odoo. ie a new table named cave.vente will be created and the all fields of sale order.line and additional two new  fields that you wanted to create will be added to this table and this table would stay as a separate table. this is not the right usage. the right way to add two new fields to the sale.order.line table with your given code is as below.


class SaleOrderLine(models.Model): 
    _inherit = 'sale.order.line' 

    @api.model 
    def _calcul_qt_vendu(self): 
​        qty = 0 
        for rec in self: 
            qty= rec.qte_sortie - rec.qte_retournee 
            rec.update({'product_uom_qty': qty})


     qte_sortie = fields.Integer('Quantité chargée') 
     qte_retournee = fields.Integer('Quantité retournée') 
     product_uom_qty = fields.Integer('Quantité commandée', compute='_calcul_qt_vendu', store=True, readonly=True)


############################################

Now after writing this code, in order to make these fields visible in the sale order  form view , you will need to inherit the sale order xml view definition and using xpath statement, you will need to put these fields in sale order line.


 


Avatar
Discard