콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
3813 화면

I have 2 models which are related with Many2Many.

When I create Model Halle, I want to automatically create 1 Model Block with it. This is my current attempt


@api.model
def create(self, values):
vals = {
'halle_id': values['id'],
}
record = self.env['rectanglemaps.block'].create(vals)
values['block_ids'] = [(4, record.id)]
return super(Halle, self).create(values)

I also tried this

@api.model
def create(self, values):
vals = {
'halle_id': self.id,
}
record = self.env['rectanglemaps.block'].create(vals)
values['block_ids'] = [(4, record.id)]
return super(Halle, self).create(values)


With my current attempt I get this error:


Traceback (most recent call last): File "/Users/jgs/jgs/Coding/Projects/odoo15/odoo/odoo/addons/base/models/ir_http.py", line 237, in _dispatch result = request.dispatch() File "/Users/jgs/jgs/Coding/Projects/odoo15/odoo/odoo/http.py", line 687, in dispatch result = self._call_function(**self.params) File "/Users/jgs/jgs/Coding/Projects/odoo15/odoo/odoo/http.py", line 359, in _call_function return checked_call(self.db, *args, **kwargs) File "/Users/jgs/jgs/Coding/Projects/odoo15/odoo/odoo/service/model.py", line 94, in wrapper return f(dbname, *args, **kwargs) File "/Users/jgs/jgs/Coding/Projects/odoo15/odoo/odoo/http.py", line 348, in checked_call result = self.endpoint(*a, **kw) File "/Users/jgs/jgs/Coding/Projects/odoo15/odoo/odoo/http.py", line 916, in __call__ return self.method(*args, **kw) File "/Users/jgs/jgs/Coding/Projects/odoo15/odoo/odoo/http.py", line 535, in response_wrap response = f(*args, **kw) File "/Users/jgs/jgs/Coding/Projects/odoo15/odoo/addons/web/controllers/main.py", line 1347, in call_kw return self._call_kw(model, method, args, kwargs) File "/Users/jgs/jgs/Coding/Projects/odoo15/odoo/addons/web/controllers/main.py", line 1339, in _call_kw return call_kw(request.env[model], method, args, kwargs) File "/Users/jgs/jgs/Coding/Projects/odoo15/odoo/odoo/api.py", line 462, in call_kw result = _call_kw_model_create(method, model, args, kwargs) File "/Users/jgs/jgs/Coding/Projects/odoo15/odoo/odoo/api.py", line 442, in _call_kw_model_create result = method(recs, *args, **kwargs) File "", line 2, in create File "/Users/jgs/jgs/Coding/Projects/odoo15/odoo/odoo/api.py", line 391, in _model_create_single return create(self, arg) File "/Users/jgs/jgs/Coding/Projects/odoo15/odoo/addons/rectanglemaps/models/halle.py", line 85, in create 'halle_id': values['id'], Exception


What am i doing wrong?        

아바타
취소
베스트 답변

@api.model

def create(self, values):

    block_id_data = {...} # Fill necessary fields here if no fields live empty like {}
    values['block_ids'] = [(0, 0, block_id_data)]
    return super(Halle, self).create(values)

아바타
취소
작성자

Thank you, this solved the problem.
While we're at it, do you mind explaining what the two zeros in the second line in the tuple mean?
Thank you for helping.

@Josef the three values of the tuple are: action_type, id, dictianary_data.
in this case
- action_type 0 means a new relation shoud be created.
- id is 0 because the object should be created and is not already here to have it's id
check here for more information: https://www.odoo.com/forum/help-1/how-to-fill-many2many-or-many2one-fields-while-creating-a-record-in-another-model-128503

작성자

@Arian Shariat Thanks!

관련 게시물 답글 화면 활동
1
8월 24
2114
0
4월 22
2283
2
7월 19
8556
0
2월 23
1750
0
1월 23
1535