如何在多对多关系字段中以及在上述字段更改时在树视图更新字段中动态更新字段 [英] How to update field dynamicaly in a many2many relation field and within tree view update field on change of above field

查看:98
本文介绍了如何在多对多关系字段中以及在上述字段更改时在树视图更新字段中动态更新字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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',

在此视图中,我已发表评论,我希望在上述ratecard_multiple类的schedule_for更改时更新此字段,以更新ratecard_sin_radio的预定字段

Within this view i have made a comment where i want this field updated onchange of above scheduled_for of ratecard_multiple class updates the scheduled field of ratecard_sin_radio

下面的视图属于ratecard_multiple

This view below belongs to 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自动更新
这段代码会在尝试更新字段时清除数据

Using this code how can i do the many2many autoupdate on the field
This code wipes out the data as it tries to update the fields

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

我按照我的建议进行了编码,但并未引起任何疑问

I did as advised i coded this function but its not firing any idea why

 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

推荐答案

示例

user_rel_id =(4,[course_ids])

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

    u可以尝试这样做,因为在新的api中,onchange不需要重新运行dict,而只需更改字段的值即可.

    u can try this because in new api onchange not need retrun dict just assing value of the fields.

    self.many2many_ids = ids
    

    这篇关于如何在多对多关系字段中以及在上述字段更改时在树视图更新字段中动态更新字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆