Odoo Help


Add automatically two articles for each Order

on 2/4/15, 6:13 AM 1,459 views


First, thank you for your time.


I would like to add automatically two articles for each Order. 

One: Transport. The second one: Services.

I would like after use the button create new order see directly these two articles on the order.

I try to make an insert at the beginning:

cr.execute('insert into sale_order_line( product_uom, order_id, price_unit, product_uom_qty,name,state,type,delay ) values (4, sale_id,1,1, \'transport\',\'draft\',\'make_to_order\',7)')

But it works only after I save the order. I suppose that before saving the order, we don't have sale_id no?


I try to create an function call by default:

    _defaults = {
        'date_order': fields.date.context_today,
        'order_policy': 'manual',
        'order_line' : _get_default_order_line,


    def _get_default_order_line(self, cr, uid, ids, context=None):
        context = context or {}
        arning = {}
        result = {}
        domain = {}
        warning_msgs = ''
        return result


But it doesn't work too :-(.

The error is:

Uncaught TypeError: Object #<Object> has no method 'slice'


Please, someone has an idea?


Thank you a lot,




Cyril Gaspard (GEM)

--Cyril Gaspard (GEM)--

| 5 6 8
Paris, France
--Cyril Gaspard (GEM)--

Forum contributor since 2008

Cyril Gaspard (GEM)
On 2/5/15, 8:27 PM


solution 1:

just override ceate method,

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

    context = context or {}

    sol = self.pool.get('sale.order.line')

    res = super(sale_order, self).create(cr, uid, vals, context=context)

    # res is the id of the created sale order, and we create 2 lines with product_1 and product_2

    self.write(cr, uid, [res], {'order_line': [[0, 0, {'product_id': id_of_product_1}], [0, 0, {''product_id}: id_of_product_2]]})

    # we must again do the onchange for each line (perhaps after the call of the onchange write the returned result in each line, I have not tested)

    for line in self.browse(cr, uid, res, context=context).order_line:

         sol.onchange_product_id(cr, uid, [line.id], line.product_id.id, ............) # see the complete method in module sale

    return res

solution 2: to display directly when you create a new record (code not tested)

override method default_get :

def default_get(self, cr, uid, fields, context=None):

    context = context or {}

    res = super(sale_order, self).default_get(cr,uid, fields, context=context)

    if 'order_line' in fields:

        res.update({'order_line': [(0, 0, {'product_id': id_of_product_1}), (0, 0, {''product_id: id_of_product_2})]})

    return res

for product_1 and product_2, if they are imported and having xml_id, define id of this products first by using their xml_id (to get the id of record which has an xml_id, use method get_object_reference)

it is better when you override a method, to call in this method a new created method in which you do your treatment.



Thank you! It works! I just didn't put the line sol.onchange_product_id(cr, uid, [line.id], line.product_id.id, ............). But when I am saving the order, I see two new lignes ;-). Just to know, it is not possible to see the two new product just after creation and not after saving the order? Thank you! Selverine

on 2/14/15, 10:52 AM

Hi, I had a second solution by overriding method default_get, but I didn't tested it, but should work. Hoping this will work. Bye

Cyril Gaspard (GEM)
on 2/19/15, 7:43 PM


| 6 5 7
Lodz, Poland

On 2/5/15, 11:05 AM
  1. Module delivery allows you to add delivery methods and cost in sale orders, as sale order line.
  2. If you want to be sure to add both, at one moment, see as it was done in the module delivery, method delivery_set()


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: 2/4/15, 6:13 AM
Seen: 1459 times
Last updated: 3/17/15, 7:32 PM