如何在many2many关系字段和树视图更新字段中更新字段动态更新上述字段

时间:2015-12-15 10:12:36

标签: openerp odoo-8 openerp-8

    class ratecard_multiple(models.Model):
    # pudb.set_trace()
    _name = 'ratecard.multiple'
    _rec_name = 'display_name'
    name = fields.Char(string='Multiple RateCard Product  Name ', required=True)
    code = fields.Char(string='Multiple RateCard Code ', readonly=True)
    scheduled_for = fields.Integer(string='SCHEDULED FOR', default=1, track_visibility='always', store=True)
    # scheduled_for  = fields.Integer(string='SCHEDULED FOR',  compute='_compute_scheduled_for',default=1 ,track_visibility='always',store=True)
    min_weeks = fields.Integer(string="MINIMUM NO OF WEEKS", default=1, store=True)
    max_weeks = fields.Integer(string="Maximum NO OF WEEKS", default=1, track_visibility='always', store=True)

    multiple_ratecard_id = fields.Many2many(comodel_name='ratecard.sin.radio',
                                            relation='ratecard_multiple_singular_rel',
                                            column1='ratecard_multiple_id',
                                            column2='ratecard_sin_radio_id',
                                            string='RATECARDS')

class ratecard_sin_radio(models.Model):
    _name = 'ratecard.sin.radio'
    _description = 'RATECARD SINGULAR RADIO  '

    code = fields.Char(string='RADIO SINGULR RATECARD CODE', readonly=True)
    name = fields.Char(string='NAME')
    outlet_id = fields.Many2one(comodel_name='outlet', string='Outlet')
    timeband_id = fields.Many2one(comodel_name='timeband', string='TimeBand')
    scheduled = fields.Integer(String='UpdatedFromSCheduledForOfRatecardMultiple')
   radio_scheduled_for =  fields.Integer(compute='onchange_scheduled' ,  string='SCHEDULED FOR ', track_visibility='always')
   update_code =  fields.Char(compute='onchange_scheduled' ,string='UPDATED CODE OF  MULTIPLE' ,track_visibility='always')

    def action_four_weeks_schedule_form(self,cr,uid,ids,context):
        order_obj = self.pool.get('ratecard.multiple').browse(cr,uid,ids)[0]
        return {
            'name':_("Four  Week Schedule to Process"),
            'view_mode': 'form',
            'view_id': False,
            'view_type': 'form',
            'res_model': 'four.weeks.schedule',
            'context':{'default_scheduled_for': order_obj.scheduled_for, 'default_code': order_obj.code},
            'type': 'ir.actions.act_window',

在这个视图中,我发表了一个评论,我希望更新此字段更新上面的schedulecard_ofultipy类更新ratecard_sin_radio的预定字段

以下视图属于ratecard_multiple

<tree editable="bottom">
    <group colspan="2" col="2">
        <field name="name" />
        <field name="display_name" />
        <field name="code" />
        <field name="validity_date" />
        <field name="scheduled_for" />
        <field name="min_weeks" />
        <field name="max_weeks" />
        <!--<field name="multiple_ratecard_id_count"/>-->
        <field name="multiple_ratecard_id_count" widget="statinfo" />
        <!--<field name="allocate_schedule_count"/>-->
        <!--<field name='company_id'/>-->
    </group>
    <field name="multiple_ratecard_id" nolabel="1" options="{'reload_on_button': true}">
        <!-- widget="many2many"-->
        <tree string="ALLOCATE SPOTS" editable="bottom">

        How  can  i  change  the  value  of  a  field  here  scheduled dynamically  with the  change in  the  field of the  above scheduled_for above noting  here  we are  in  the many2many  multiple_ratecard_id


<field name="code" />
            <field name="name" />
            <field name="outlet_id" on_change="onchange_outlet(outlet_id)" placeholder="OUTLET" />
        </tree>
    </field>
</tree>

使用此代码,我如何在字段上执行many2many自动更新 此代码在尝试更新字段

时清除数据
@api.one
@api.onchange('multiple_ratecard_id',)
@api.depends('multiple_ratecard_id','multiple_ratecard_id.scheduled_for')
def onchange_scheduled(self):
    updated_scheduled_for = 0
    update_multiple_code  = ''
    obj = self.env['ratecard.multiple'].browse(self.ids)
    for  lineitems  in  obj:
        print '#########################################'
        print 'Objects  in  ratecard.multiple' , lineitems[0]
        print  'NAME' ,  lineitems.name
        print 'CODE' , lineitems.code
        print 'SCHEDULED CODE ', lineitems.scheduled_for
        print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% RATECARD.MULTIPLE'
        updated_scheduled_for =  lineitems.scheduled_for
        print  'RADIO SCHEDULED UPDATED  WITH  VALUE' ,updated_scheduled_for
        update_multiple_code = lineitems.code
        print  'UPDATE  CODE '  , update_multiple_code
        print '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'

    lineitems.update({
        'radio_scheduled_for': updated_scheduled_for,
        'update_code': update_multiple_code ,
    })

我按照建议做了我编码这个功能,但它不解释为什么

 multiple_ratecard_id = fields.Many2many(comodel_name='ratecard.sin.radio',
                                        relation='ratecard_multiple_singular_rel',
                                        column1='ratecard_multiple_id',
                                        column2='ratecard_sin_radio_id',
                                        string='RATECARDS')
@api.one
@api.onchange('scheduled_for','code')
@api.depends('multiple_ratecard_id.radio_scheduled_for','multiple_ratecard_id.update_code')
def onchange_scheduled(self):
    for  lines  in  self:
        for  lineitems in  lines.multiple_ratecard_id:
            lineitems.radio_scheduled_for = lines.scheduled_for
            print  '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'
            print  'UPDATED RADIO SCHEDULED == ' , lineitems.radio_scheduled_for
            lineitems.update_code  = lines.code
            print  'UPDATED RADIO CODE  === ' , lineitems.update_code

1 个答案:

答案 0 :(得分:0)

示例

user_rel_id =(4,[course_ids])

write({'many2many_ids': [user_rel_id]})
  • 对于many2many字段,需要一个元组列表。

DOC阅读

(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

DOC

你可以试试这个,因为在新的api onchange中不需要retrun dict只是在分配字段的值。

self.many2many_ids = ids