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

How to create/insert multiple row in single action?

By
yopi
on 1/15/15, 3:14 AM 1,634 views

I have some case where single data from form submitted have posibitilies become multiple row (data/dicts) after manipulation. I am trying to override create method and put it in loop, but after test in website, i got warning message like this.

@api.returns('self', lambda value: value.id)

AttributeError: 'list' object has no attribute 'id'

here my code :

class Test(models.Model):

...

@api.model

def create(self, values):

test_id = self._manipulate_data(values)

res_id = []

if len(test_id) > 0:

for value_id in test_id:

res_id.append(super(Test, self).create(value_id))

return res_id

return super(Test, self).create(values)

res_id containing values like this : [test(160,), test(161,), test(162,)]

0

Bole

--Bole--
2385
| 5 4 6
Zagreb | Karlovac, Croatia
--Bole--

there is only 10 kind of people the ones that understand binary ... and others

Bole
On 1/15/15, 3:37 AM

here is what happend in pseudo code (human readable translated) :

- original create method recived values, 
- then you 'manipulate' those valuse and put it in test_id ( by naming convention it should be _ids.. butok) 
- after that you check if you have elements in test_id list and for each value in list you try to call create...
that is no good... 
 

if create recived values (in whatever form : list, dict tuple.. ) 
you super class call should be passed the same form of vals... 

in your case : recived values ( one variable, ) returning correct super call only if test_id has no values.. 
but if it has some vals.. it trys to return a list of super calls... 

think again and modify your _manipulate_data method to do whatever, but one the data is ready for next step.. it should be in same form (modified or not) as the entered.. 


one more thing , and this might be the most important part... 
create method can be run on ONE record only, and it MUST return ONE ID of newly created recor, or False if record was not created...
 

hope it helps :)

0
yopi
On 1/15/15, 4:44 AM

Bole, thanks a lot for advice. :)

I desperate after doing lot of experiments for this case. Before pushing the creation in create method, i implemented it in function workflow but it seems not work too (in function workflow, i put self.create(data_manipulate) inside loop. After debug, i found that the original data submitted from form is process first followed my manipulated data).

i make some modification, like this:

@api.model

def create(self, values):

test_id = self._manipulate_data(values)

if len(test_id) > 0:

for value_id in test_id:

res_id = super(Test, self).create(value_id)

return res_id

return super(Test, self).create(values)

this code is work, but i dont know, it's a good code or bad code.

well .. you should try writing a method wich is not create, but some other.. in wich you will loop / iterate over some data, modify/prepare data.. and from that method ( from loop or outside loop) call create method.. (just pass ready vals to create... that would be preffered way to achieve what yu need...

Bole
on 1/15/15, 5:03 AM

thanks bole, i will try it :)

yopi
on 1/15/15, 5:26 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/15/15, 3:14 AM
Seen: 1634 times
Last updated: 3/16/15, 8:10 AM