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.

1

What is KeyError: "Field '0' does not exist?

By
Anirudh Lou
on 1/21/15, 4:46 AM 1,545 views

Hi everyone, i have create def create that overrides how the create button behave but it returns an error. Any help is very much appreciate. Here is the chunk of my code.

...

pr_id = vals.get('purchase_requisition_id')

order_id = self.pool.get('purchase.order').search(cr,user,[('requisition_id','=',pr_id)],context=context)

for purchase_order in self.pool.get('purchase.order').browse(cr,user,order_id):

            vals['stocks_ready_received'] = purchase_order.stocks_ready_received

            vals['stocks_received'] = purchase_order.stocks_received

. . .

 

but when vals['stocks_read_received'] = purchase_order.stocks_ready_received is read it pops up an error something like this:

 

 File "/opt/openerp/server-7/openerp/osv/orm.py", line 4469, in create
   result += self._columns[field].set(cr, self, id_new, field, vals[field], user, rel_context) or []
 File "/opt/openerp/server-7/openerp/osv/fields.py", line 555, in set
   if act[0] == 0:
 File "/opt/openerp/server-7/openerp/osv/orm.py", line 383, in __getitem__
   raise KeyError(error_msg)
KeyError: "Field '0' does not exist in object 'browse_record(stock.move, 10)'"

1

Ivan

--Ivan--
3210
| 5 3 6
Jakarta, Indonesia
--Ivan--
Ivan
On 1/21/15, 5:36 AM

@Anirudh, is stocks_ready_received by any chance a one2many or many2many field?  If so, you need to change it to something like: vals['stocks_ready_received'] = [(6, 0, [x.id for x in purchase_order.stocks_ready_received])]

They are one2many field sir.

Anirudh Lou
on 1/21/15, 5:39 AM

Sir, i have tried your suggestion, during running it does not produce any error but it also output nothing, still there is no value bieng passed on vals['stocks_ready_received']. With all due respect, me i ask, what do 6 and 0 mean?

Anirudh Lou
on 1/22/15, 12:28 AM

Ah, yeah it produce constraints error. It says: The operation cannot be completed, probably due to the following: - deletion: you may be trying to delete a record while other records still reference it - creation/update: a mandatory field is not correctly set [object with reference: Purchase Order - purchase.order]

Anirudh Lou
on 1/22/15, 12:30 AM

The 6 and 0 are related to OpenERP's way to populate one2many and many2many fields. You can read more about it in the following answer: https://www.odoo.com/forum/help-1/question/bug-o2m-lines-populated-from-onchange-event-gets-appended-rather-than-overriding-when-saving-62734#answer_62751 or in the code itself: odoo/odoo/openerp/osv/fields.py (search for class many2many or class one2many). If your field is one2many, it cannot accept operation 6, you need to add it one by one: vals['stocks_ready_received'] = [(4, x.id ) for x in purchase_order.stocks_ready_received].

Ivan
on 1/22/15, 12:46 AM

Sir, why it response for an integrity error? Does it mean that purchase_order.stocks_ready_received had no purchase order?

Anirudh Lou
on 1/22/15, 2:02 AM

Not sure, but I guess because when you do operation 6, it actually deletes the relationship in many2many first. That might be the cause as in many2many you have a bridging table whereas in one2many you don't.

Ivan
on 1/22/15, 3:09 AM

Did it work with the 4 operation?

Ivan
on 1/22/15, 3:09 AM

It prompt a constraints error on purchase.order class sir.

Anirudh Lou
on 1/26/15, 5:11 AM

What constraint error?

Ivan
on 1/26/15, 5:43 AM

This is what it said: The operation cannot be completed, probably due to the following: - deletion: you may be trying to delete a record while other records still reference it - creation/update: a mandatory field is not correctly set [object with reference: Purchase Order - purchase.order]

Anirudh Lou
on 1/28/15, 2:40 AM

Is the piece of code that you posted in the question is called before you call the super to the create method? If so, then the PO would not have been created yet.

Ivan
on 1/28/15, 4:13 AM

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: 1/21/15, 4:46 AM
Seen: 1545 times
Last updated: 3/16/15, 8:10 AM