Odoo Help

Welcome!

This community 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.

0

overwriting methods create/write with one2many object

By
N
on 5/14/13, 9:29 PM 4,310 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),
}
4

Sandeep

--Sandeep--
6083
| 6 5 8
Delhi, India
--Sandeep--
Python OpenERP Developer http://in.linkedin.com/pub/sandeep-rana/3b/a16/786/
Sandeep
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):

id=super(sale_order,self).create(cr,uid,vals,context)</t>
cid=self.pool.get('sale.order.section').create(cr,uid,{'order_id':id,'number':'sec0001'})
for i in self.browse(cr,uid,id).order_line:

self.poo.get('sale.order.line').write(cr,uid,[i.id],{'section_id':cid})
return id

Thanks

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

N
on 5/15/13, 4:02 AM

in your inherited sale.order

Sandeep
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

N
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

Sandeep
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

N
on 5/15/13, 5:22 AM

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

Sandeep
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

N
on 5/15/13, 5:57 AM
0
vim24
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.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

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