Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
1 Balas
3322 Tampilan

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?        

Avatar
Buang
Jawaban Terbai

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

Avatar
Buang
Penulis

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

Penulis

@Arian Shariat Thanks!

Post Terkait Replies Tampilan Aktivitas
1
Agu 24
1537
0
Apr 22
1880
2
Jul 19
8158
0
Feb 23
1394
0
Jan 23
1223