问题
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',
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
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>
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
回答1:
EXAMPLE
user_rel_id = (4,[course_ids])
write({'many2many_ids': [user_rel_id]})
- For a many2many field, a list of tuples is expected.
DOC Read It
(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 can try this because in new api onchange not need retrun dict just assing value of the fields.
self.many2many_ids = ids
来源:https://stackoverflow.com/questions/34286371/how-to-update-field-dynamicaly-in-a-many2many-relation-field-and-within-tree-vie