Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
16850 Widoki

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

Awatar
Odrzuć
Najlepsza odpowiedź

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 :)

Awatar
Odrzuć
Autor Najlepsza odpowiedź

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.

Awatar
Odrzuć

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

Autor

thanks bole, i will try it :)

Powiązane posty Odpowiedzi Widoki Czynność
1
lip 15
3336
2
gru 20
8235
1
kwi 24
579
2
gru 23
23287
2
sty 16
3837