Help

6

How to insert value to a one2many field in table with create method?

Avatar
Gopakumar N G

I have created two wizards which are shown below:

from osv import fields, osv
class fee_structure(osv.osv_memory):
_name="fee.structure"
_description="Fee Structure"
_columns={
            'name': fields.char('Title', size=64),
            'area_id': fields.many2one('department.area', 'Area'),
            'grade_id': fields.many2one('department.grade', 'Grade'),
            'fee_template_ids': fields.many2many('department.fee_template', 'fee_structure_template_rel', 'fee_structure_id', 'dept_fee_template_id', 'Templates'),
            'fee_ids': fields.one2many('wizard.fee', 'fee_structure_id','Extra Fee'),
          }

def button_create(self, cr, uid, ids, context={}):
    print '..................button_create................'
    wizard_obj=self.browse(cr, uid, ids, context=context)[0]
    template_ids=[]
    for template_id in wizard_obj.fee_template_ids:
        template_ids.append(template_id.id)
self.pool.get('institution.fee').create(cr, uid, {
                                'name': wizard_obj.name,
                                 'active': True,
                                  'area_id': wizard_obj.area_id.id,
                                   'grade_id': wizard_obj.grade_id.id,
                                   'fee_template_ids': [(6,0,template_ids)],
                                #  needs to add values for field fee_ids here,
                                                      })
fee_structure()

class wizard_fee(osv.osv_memory):
_name="wizard.fee"
_description="Fee Wizard"
_columns={
            'fee_id': fields.many2one('fee.fee', 'Fee'),
            'amount': fields.float('Amount'),
            'fee_structure_id': fields.many2one('fee.structure',  'Fee Structure'),
          }

wizard_fee()

The model institution.fee is

class institution_fee(osv.osv):
_name="institution.fee"
_description="Institution Fee"
_columns={
            'name': fields.char('Fee Name', size=64),
            'active': fields.boolean('Active'),
            'area_id': fields.many2one('department.area', 'Area'),
            'grade_id': fields.many2one('department.grade', 'Grade'),
            'fee_template_ids': fields.many2many('department.fee_template', 'institution_fee_template_rel', 'inst_fee_id', 'dept_fee_template_id', 'Fee Template'),
            'fee_ids': fields.one2many('department.fee_structure', 'inst_fee_id', 'Extra Fee'),
          }
_defaults={
            'active': True
           }
institution_fee()

I have added the values of the many2one field fee_template_ids to model institution.fee , similarly I want to add the values to the one2many field fee_ids using wizard.fee to institution.fee .

The fee.structure wizard is called from a menuitem and its use is to create a fee structure(institution.fee model).

Avatar
Discard
4 Answers
33
Avatar
nazarii
Best Answer

This may help you:

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

In your case you need to use (0, 0, { values })

4 Comments
Avatar
Discard
Avatar
Gopakumar N G
-

Thanks nazarii..

Avatar
Carlos Llamacho
-

The only option that takes a list is number 6, right? I cannot do a (4, [id, id, ids]) and have it all been related with my record? Or a (0, 0, [{values}])?

Avatar
nazarii
-

If you want to link new record to existing wizards than yes, you need to use (6, 0, [IDS]). But if you want to create new wizards on the fly and link to this record than use 'fee_ids': [(0, 0, values1), (0, 0, values2) ]

Great

5
Avatar
kirubanidhi
Best Answer

Insert a new record in one2many fields in odoo9.

Code:

@api.multi

def Create_One2many_method(self):

search_var = self.search([('staff_age','=',0)])

search_var.write({

                    'stud_ids': [(0,0, {

                                                'reg_no':4200,

                                                 'stud_email':'anbulove@gmail.com',

                                                   'stud_phone':'9788987689',

 

})]

})

 

Avatar
Discard
0
Avatar
Abdelhalim
Best Answer

i want to explain how odoo framework create related table for many2many for exemple we have your model1 where you created "category_id" and the other model2 where you Create the relation with many2many

Now for each models odoo create table in Psql , so for models1 odoo create in Psql table:model1and for the second model Psql create table: Model2 and for the relation many2many Psql create the third his name is table:model1_Model2

so in odoo create this table with underscore and the 2 table Name

Avatar
Discard
0
Avatar
Annie Marinna
Best Answer

In my case, the wizard should add or relate one or more existing records into the "one2many" field of the new record. Then if I'm not wrong, what I must to do is a (1, ID, { values }) with ID = the id of the fresh new record, right?

1 Comment
Avatar
Discard
Avatar
Annie Marinna
-

but this is what I get :( AttributeError: "'list' object has no attribute 'copy'"