0

I have 2 tables with the one2many relation. I want to create a set of records in the slave table after changing a name value in master table. But I have an error:

File "C:\odoo80\openerp\addons-custom\att\agreement.py", line 18, in _onchange_name

self.pool['att.agreement.line'].create({'agreement_id': id , 'name': str(x), 'qty': x * 100} )

File "C:\odoo80\openerp\api.py", line 250, in wrapper return old_api(self, *args, **kwargs)

TypeError: create() takes at least 4 arguments (2 given)

My classes are:

    class Agreement(models.Model):
    _name = 'att.agreement'
    line_id = fields.One2many('att.agreement.line','agreement_id', 'Lines', copy=True )
    name = fields.Char(string='Name', required=True)

    def create(self, cr, uid, values, context=None):
        return super(Agreement, self).create(cr, uid, values, context)

    @api.onchange('name')
    def _onchange_name(self):
        if (self.name):
           for x in range(1,5,1):
               self.pool['att.agreement.line'].create({'agreement_id': id , 'name': str(x), 'qty': x * 100} )

    class AgreementLine(models.Model):
    _name = 'att.agreement.line'
    agreement_id = fields.Many2one('att.agreement', ondelete='cascade', select=True ) # required=True ,readonly = True
    name = fields.Char(string='Name', required=True)
    qty  = fields.Integer(string = 'Qty')

    def create(self, cr, uid, values, context=None):
        id = super(AgreementLine, self).create(cr, uid, values, context)
        return id

My view is:

<?xml version="1.0" encoding="UTF-8"?>
     <openerp>
        <data>
            <!-- form AGREEMENT -->
            <record model="ir.ui.view" id="att_agreement_view_form">
                <field name="name">Agreement</field>
                <field name="model">att.agreement</field>
                <field name="arch" type="xml">
                <form string="Agreement">
                        <sheet>
                            <group colspan="4">
                                <group>
                                    <field name="name"/>
                                </group>
                            </group>
                             <notebook>
                                    <page string="Agreement Lines">
                                        <tree string="Agreement Lines"  create ='false'>
                                            <field name="line_id" >
                                                    <field name = "id"/>
                                                    <field name="name"/>
                                                    <field name="qty"/>
                                            </field>
                                        </tree>
                                    </page>
                             </notebook>
                        </sheet>
                </form>
                </field>
            </record>

            <record model="ir.ui.view" id="att_agreement_view_tree">
                <field name="name">List of agreement</field>
                <field name="model">att.agreement</field>
                <field name="arch" type="xml">
                    <tree string="List of agreements">
                        <field name="name"/>
                    </tree>
                </field>
            </record>

            <!-- window action -->
            <record model="ir.actions.act_window" id="att_agreement_list_action">
                <field name="name">Agreement</field>
                <field name="res_model">att.agreement</field>
                <field name="view_mode">tree,form</field>
            </record>

            <menuitem id="att_agreement_menuitem" name="Соглашения"
                      parent="att_menu_agreement"
                      action="att_agreement_list_action"/>
        </data>
    </openerp>

Can anybody help me?

SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
atimtim
  • 1
  • 5

4 Answers4

0

You are mixing API 8 and API 7 versions of Odoo. In your case you are working with API 8, so don't use pool. Use env instead:

self.env['att.agreement.line'].create({'agreement_id': id , 'name': str(x), 'qty': x * 100})
forvas
  • 9,801
  • 7
  • 62
  • 158
0

Thanks! I've rewrited method like this:

   @api.onchange('name')
   def _onchange_name(self):
       if self.name:
           if isinstance(self.id, models.NewId):
               for x in range(1,6,1):
                   self.line_id.create({'agreement_id': self.id, 'name': str(x), 'qty': x * 100})

I've got the code without runtime errors. But i have another problem now - slave records were created without links with a master record (the agreement_id field is empty in a AgreementLine table). It happened because the master record wasn't saved before creation slave records. But i need to create slave records after on_change event, before the master record will be saved in database. On the contrary, if i use the list button "add an item" on the view i get a correct result (i see the added element in the list, but nothing in database) I don't know, how to resolve this problem...

P.S. I need a behavior like a creation slave records with clicking button "add an item", but in program code.

atimtim
  • 1
  • 5
0
class Agreement(models.Model):
    _name = 'att.agreement'
    line_id = fields.One2many('att.agreement.line','agreement_id', 'Lines', copy=True )
    name = fields.Char(string='Name', required=True)

    @api.model
    def create(self,  values):
        return super(Agreement, self).create( values)

    @api.onchange('name')
    def _onchange_name(self):
        if (self.name):
           for x in range(1,5,1):
               self.env['att.agreement.line'].create({'agreement_id': self. id , 'name': str(x), 'qty': x * 100} )




 class AgreementLine(models.Model):
    _name = 'att.agreement.line'
    agreement_id = fields.Many2one('att.agreement', ondelete='cascade', select=True ) # required=True ,readonly = True
    name = fields.Char(string='Name', required=True)
    qty  = fields.Integer(string = 'Qty')

    @api.model
    def create(self, values):
        return super(AgreementLine, self).create( values)
Prashant
  • 704
  • 10
  • 23
  • I'm sorry, but a result is the same – atimtim Nov 04 '15 at 08:02
  • i've put a code and a view. But i've moved a problem code from on_change method to create method... – atimtim Nov 04 '15 at 11:56
  • Hi! Thank you very match. But you did't modify an on_change method. Your code work after create event. I need a creation slave record after on_change method. I've just read an article https://www.odoo.com/forum/help-1/question/create-new-many2one-records-in-api-onchange-method-69837 – atimtim Nov 04 '15 at 13:04
  • Hi! Thank you! But you did't modify an on_change method. Your code works after create event. I need a creation slave record after on_change method. I've just read an article https://www.odoo.com/forum/help-1/question/create-new-many2one-records-in-api-onchange-method-69837 . Someone had the same problem. But i haven't understood yet how to make like that in my code – atimtim Nov 04 '15 at 13:39
  • hello dear but it's not a good think to create in onchange method ! when use onchange method if u return value or set the value not create or write then use onchange method without don't use and here u also used create method so why use to create in onchange method – Prashant Nov 04 '15 at 16:56
  • For example, you have a partner which has a list of products. You need to create this list in a form after choice a partner. It's real life example. – atimtim Nov 05 '15 at 07:12
  • @api.onchange('name') def _on_change_name(self): if self.name: self.qty += 100 print 'The on_change event was happened' for x in range(1,5,1): self.slave_id += self.line_id.new({'name': str(x), 'qty': x}) – atimtim Nov 05 '15 at 13:05
  • @atimtim "new" creates a temporary record to apply the on_change afterwards................. so u can use it............... – Prashant Nov 06 '15 at 05:51
0

this code makes all job: self.line_id += self.line_id.new({'name': str(x), 'qty': x})

@api.onchange('name')
def _on_change_name(self):
        if self.name:
            for x in range(1,5,1):
                self.line_id += self.line_id.new({'name': str(x), 'qty': x})
atimtim
  • 1
  • 5
  • "new" creates a temporary record to apply the on_change afterwards................. so u can use it............... – Prashant Nov 06 '15 at 05:51