Skip to Content
Menu
This question has been flagged

Hello,

I'm trying to pass values and show the records from the form view of model_b.py into one2many field notebook in the form view of model_a.py by wizard.


model_b.py  like this:

def accept(self):

        lstd = {

            'name': self.name.id,

            'thamchieu': self.id,

            'thoigian': self.thoigian,

            'bhxh': self.bhxh,

            'bhtn': self.bhtn,

            'bhxh_mucdongnld': self.bhxh_mucdongnld,

            'bhxh_mucdongcty': self.bhxh_mucdongcty,

            'bhtn_mucdongnld': self.bhtn_mucdongnld,

            'bhtn_mucdongcty': self.bhtn_mucdongcty,

            'bhyt_mucdongnld': self.bhyt_mucdongnld,

            'bhyt_mucdongcty': self.bhyt_mucdongcty

        }

        ls_dc = self.env['model.a']

        ls_dc = ls_dc.write({'lstd_baohiem':[(0,0,{lstd})]})


XML of one2many tree view in the form view of an user's model_a.py like this:

<notebook>

                            <page name="bao_hiem_lich_su" string="Lịch Sử Thay Đổi">

                                <field name="lstd_baohiem">

                                    <tree>

                                        <field name="thoigian"/>

                                        <field name="bhxh" string="M.Đ BHXH"/>

                                        <field name="bhtn" string="M.Đ BHTN"/>

                                        <field name="bhxh_mucdongnld"/>

                                        <field name="bhxh_mucdongcty"/>

                                        <field name="bhyt_mucdongnld"/>

                                        <field name="bhyt_mucdongcty"/>

                                        <field name="bhtn_mucdongnld"/>

                                        <field name="bhtn_mucdongcty"/>

                                        <field name="loai"/>

                                        <field name="thamchieu"/>

                                    </tree>

                                </field>

</page>

</notebook>


After clicking the button 'Accept'. I got the issue like that:

Odoo Server Error
Traceback (most recent call last):
  File "/home/odoo/src/odoo/odoo/http.py", line 624, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/home/odoo/src/odoo/odoo/http.py", line 310, in _handle_exception
    raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
  File "/home/odoo/src/odoo/odoo/tools/pycompat.py", line 14, in reraise
    raise value
  File "/home/odoo/src/odoo/odoo/http.py", line 669, in dispatch
    result = self._call_function(**self.params)
  File "/home/odoo/src/odoo/odoo/http.py", line 350, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/home/odoo/src/odoo/odoo/service/model.py", line 94, in wrapper
    return f(dbname, *args, **kwargs)
  File "/home/odoo/src/odoo/odoo/http.py", line 339, in checked_call
    result = self.endpoint(*a, **kw)
  File "/home/odoo/src/odoo/odoo/http.py", line 915, in __call__
    return self.method(*args, **kw)
  File "/home/odoo/src/odoo/odoo/http.py", line 515, in response_wrap
    response = f(*args, **kw)
  File "/home/odoo/src/odoo/addons/web/controllers/main.py", line 1331, in call_button
    action = self._call_kw(model, method, args, kwargs)
  File "/home/odoo/src/odoo/addons/web/controllers/main.py", line 1319, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "/home/odoo/src/odoo/odoo/api.py", line 387, in call_kw
    result = _call_kw_multi(method, model, args, kwargs)
  File "/home/odoo/src/odoo/odoo/api.py", line 374, in _call_kw_multi
    result = method(recs, *args, **kwargs)
  File "/home/odoo/src/odoo/addons/baohiem/models/dieu_chinh.py", line 128, in chapthuan
    ls_dc = ls_dc.write({'lstd_baohiem':[(0,0,{lstd})]})
TypeError: unhashable type: 'dict'

What am I doing wrong?

Please help!

Thank you!

Avatar
Discard
Best Answer


(0, 0, { values }) link to a new record that needs to be created with the given values dictionary (1, ID, { values }) update the linked record with id = ID (write *values* on it) (2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well) (3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself) (4, ID) link to existing record with id = ID (adds a relationship) (5) unlink all (like using (3,ID) for all linked records) (6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)

Check the above format,Confirm the error comes in some extra brackets or wrong rules you defined.

Example :

check the link

https://www.odoo.com/forum/help-1/not-able-to-write-datas-in-res-partner-res-partner-category-rel-table-182270

Avatar
Discard
Author Best Answer

Hello Mohamed,

Yes, I connect with model_c.py by creating a one2many field ('lstd_baohiem'), cause I want to put the tree view of model_c.py in the form view of a user of model_a.py. 

But model_c.py and model_b.py have connected by the relationship with one2many (model_b.py) and many2one (model_c.py). And I have done to pass all the value from model_b.py to the tree view of model_c.py via the KEY field of model_b.py:

reference = fields.Char('Reference')

Back to the model_a.py. I do not know how to create a many2one field for model_c.py to connect with one2many of model_a.py. 

Because the KEY field is 'reference' in model_b.py. That's the KEY field for 3 models not 2.

So that why I'm trying to copy or pass all values of the model_b.py by using dictionary like that. But still no sucess.

Avatar
Discard
Best Answer

You can prepare values same like above and add model_b 'id' in Many2one field too.

Write: model_b.create(values) method if you have already model_a record is created.

After that you can see the newly created record gets added in One2many field.

Avatar
Discard
Author

Hi Mohammed. Cause I created one2many field: lstd_baohiem from... model C not model B. So you mean that I must have created a relation one2many from model.b.py and many2one from model.a.py!?

Let you have Model_a, Model_b and Model_c. Now you have relation between Model_a and Model_b as One2many and Many2one respectively. According to your above code, I can understand that you have field 'lstd_baohiem' in Model_a. That means, relation of One2many with Model_c and Model_a as Many2one, right?

Related Posts Replies Views Activity
0
Feb 21
2912
1
Jul 22
2790
1
Mar 22
5455
3
Mar 21
4378
1
Sep 20
6584