Odoo Help


overwriting methods create/write with one2many object

on 5/14/13, 9:29 PM 4,652 views

In the view of the creation of a sale order, i put a number of section for each sale order line (a section allows me to decompose the sale order into multiple parts). When i create the sale order, i want the sale order section to be created too, with the same number i have put in the view. I've heard about overwriting create/write methods but i don't know how to do. Thanks, here is a part of my code :

class sale_order_line(osv.osv):

_name = 'sale.order.line'
_inherit = 'sale.order.line'

_columns = {
            'section_id': fields.many2one('sale.order.section', 'n° section', required=False, ondelete='cascade', select=True, readonly=True, states={'draft':[('readonly',False)]}),
            'number_section': fields.related('section_id', 'number', type='integer', string='N° section', store=False)

class sale_order_section(osv.osv):

_name = 'sale.order.section'
_columns = {
            'order_line': fields.one2many('sale.order.line', 'section_id', 'Order Lines', readonly=False),
            'number': fields.integer('n° section', help='permet de décomposer le devis en sous-sections'),
            'order_id': fields.related('order_line', 'order_id', type="many2one", relation='sale.order', string="Order", readonly=True, required=True),


| 6 5 8
Delhi, India
Python OpenERP Developer http://in.linkedin.com/pub/sandeep-rana/3b/a16/786/
On 5/15/13, 3:34 AM

Hi n@ilyk

as your Q, you can do like below :

you have inherit sale.order then put these 2 method

def create(self,cr,uid,vals,context=None):

for i in self.browse(cr,uid,id).order_line:

return id


Sorry : where i put this code ? in the sale_order_section class ?

on 5/15/13, 4:02 AM

in your inherited sale.order

on 5/15/13, 4:12 AM

ok but i don't understand where 'sec0001' come from . In fact, for each order line i have a column "number" where i put the number of section associated to the order line. Also, if i put the same number for 3 order lines for example, just one section has to be created. Thanks for your help

on 5/15/13, 4:39 AM

it was just a string which i put it , you can use your own no,,, for number in order line , i think you no need it because you have already a section Id , so why NUMBER ..... even you should use name of filed number as "name" then you can see section number in section Id , then you never need number in order line ,,,, as we have record id

on 5/15/13, 5:17 AM

Yes, it is the problem : i try to do 'number': order_line.number but it doesn't work. And a sale order is decomposed into multiples sections and here you create only one section ? I have number because if i write the same number for differents order lines, only one section is created

on 5/15/13, 5:22 AM

one sale order as one section Id am i right ??

on 5/15/13, 5:44 AM

no : one sale order has multiples sections (a section is a group of invoice lines), that is why i use a number ^^. However, a sale order line is associated with only one section

on 5/15/13, 5:57 AM
On 5/14/13, 11:20 PM

To override the create method just put your own create method in your sale.order.line class, in the following format:

    def create(self, cr, uid, vals, context=None):
         # Your additional code here
         return super(sale_order_line,self).create(cr,uid,vals,context)

The super(...) will ensure the original create method is still called and creates the object, while you code can add in any additional functionality you need.

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 5/14/13, 9:29 PM
Seen: 4652 times
Last updated: 3/16/15, 8:10 AM