Odoo Help


How to write in a field one2many? [Closed]

Anabela Damas
on 11/1/13, 12:44 PM 8,208 views

The Question has been closed

Anabela Damas
on 12/06/2013 11:35:11


I'm trying to create a relation between Stock.picking.out and account.invoice, like this:

class stock_picking_out(osv.osv):
    _name = "stock.picking.out"
    _inherit = "stock.picking.out"
    _table = "stock_picking"
    _description = "Delivery Orders"
    _columns = {
        'inv_id': fields.many2one('account.invoice', 'Invoice'),

class account_invoice(osv.osv):   
    _inherit = "account.invoice"
    def _verify_state(self, cr, uid, ids, field_name, arg, context=None):   
         result = {} 
         invoices = self.browse(cr, uid, ids, context=context)
         for invoice in invoices:
             if invoice.state == 'open':          
                 origin = invoice.origin
                 stock_picking_obj = self.pool.get('stock.picking').search(cr, uid, [('origin', '=', origin )])   
                  for line in stock_picking_obj:
                      stock_picking = self.pool.get('stock.picking').browse(cr, uid, line, context)
                      stock_picking_ids = stock_picking.id
                      self.write(cr, SUPERUSER_ID, invoice.id, {'stock_picking_ids':[(1,stock_picking_ids,context)], 'stock_picking_name': stock_picking_name, 'stock_movement_type': stock_movement_type, 'stock_date': stock_date, 'type_document': invoice.journal_id.saft_inv_type}, context=context)
 _columns = {
        'verify_state': fields.function(_verify_state, method=True, store={'account.invoice': (lambda self, cr, uid, ids, ctx={}: ids , ['state'], 10),}, string="Verify State"),
        'stock_picking_ids': fields.one2many('stock.picking.out', 'inv_id', 'Stock Picking'),

This code does the write but when I try to access the field stock_picking_ids I get this error :

  File "/opt/openerp_svn/trunk/openerp/osv/expression.py", line 804, in parse
    raise ValueError("Invalid field %r in leaf %r" % (left, str(leaf)))
ValueError: Invalid field 'inv_id' in leaf "<osv.ExtendedLeaf: ('inv_id', 'in', [5]) on stock_picking (ctx: )>"

I follow this :

(0, 0,  { values })    link to a new record that needs to be created with the given values dictionary
(1, ID, { values })    update the linked record with id = ID (write *values* on it)
(2, ID)                remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)

How can I correct this error ? And add a relation to my account.invoice?




| 3 2 6
Gandhinagar, India

Having 8+ years of OpenERP Experience. We are Specialized in OpenERP/CRM Solutions, software development, Web development, E commerce solutions & other open source solutions.

Skype : acespritech

Email : info@acespritech.com

On 11/2/13, 3:35 AM

Hi Anabela,

The error says inv_id is not in stock picking object.
You've inherited stock picking out object and added a field, same thing you've to do for stock picking object.

Like this,

class stock_picking(osv.osv):
    _inherit = "stock.picking"
    _columns = {
        'inv_id': fields.many2one('account.invoice', 'Invoice'),


With this the error has disappeared, but the fields are empty. With 'stock_picking_ids':[(1,stock_picking_ids,context)] nothing is written in the fields, so I removed the context but now I've this error :

result += self._columns[field].set(cr, self, id, field, vals[field], user, context=rel_context) or [] File "/opt/openerp_svn/trunk/openerp/osv/fields.py", line 560, in set obj.write(cr, user, [act[1]], act[2], context=context)

How do I save the fields if it isn't like this self.write(cr, SUPERUSER_ID, invoice.id, {'stock_picking_ids':[(1,stock_picking_ids,context)],context) ?

Versão Integral, Anabela Damas
on 11/4/13, 7:27 AM

I don't understand, but I fix the problem. I use this : (4, ID) link to existing record with id = ID (adds a relationship), but this is the list of tuples that is expected for a many2many field. (https://doc.openerp.com/6.0/developer/2_5_Objects_Fields_Methods/methods/)

So I use the write like this : self.write(cr, SUPERUSER_ID, invoice.id, {'stock_picking_ids':[(4,stock_picking_ids)],context)

And everything seems to be ok. I hope =) . Thanks!!!!

Versão Integral, Anabela Damas
on 11/4/13, 7:52 AM

Wow, that's good ! (4, ID) adds existing record to a many2many field with ID. And (1, ID, { values }) updates the existing record of many2many with ID.

on 11/11/13, 1:42 AM

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: 11/1/13, 12:44 PM
Seen: 8208 times
Last updated: 3/16/15, 8:10 AM