This question has been flagged
1 Reply
8085 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)'"

Avatar
Discard
Best Answer

@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])]

Avatar
Discard
Author

They are one2many field sir.

Author

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?

Author

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]

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].

Author

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

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.

Did it work with the 4 operation?

Author

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

What constraint error?

Author

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]

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.