Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
6341 Lượt xem

Hi guys,

I run on an Odoo v10, i got a situation, these are my 3 model:

allowance

name = fields.Char()

job

allowance_ids = fields.One2many('job.allowance.rel', 'job_id')

job.allowance.rel

job_id = fields.Many2one('job')
allowance_id = fields.Many2one('allowance')
value = fields.Float()

I tried to set domain on field allowance_ids to avoid duplicate allowance type but nothing work.

plz help me. Thanks!



Ảnh đại diện
Huỷ bỏ

avoiding duplicates on uncommitted changes requires a bit of work. you should add onchange event to m2o fields which sets domain for these fields on selection. here your problem will be save vs save&new button. you might have to disable save&new button.

Tác giả

@Bréndou Serge Eric: I'm using your idea too. But it not really convenient for user when number of line become larger. So I want to find a way to limited allowance just show which haven't existed

@F.P.: I tried make a domain both in python code and view but neither work

@Leo, can you show us the code for the domains you have tried ?

Tác giả

I defined an onchange function on job.allowance.rel model:

@api.onchange('job_id')

def _onchange_job(self):

ids = self.job_id.allowance_ids.mapped('allowance_id').ids

return {

'domain': {

'allowance_id': [('id', 'not in', ids)],

},

}

Câu trả lời hay nhất

Hello! I dont know how to set that kind of domain: but i know a walk around:
You will just need to write a constraint on job.allowance.rel , precisely on the fields job_id and allowance_id.

_sql_constraints = [
('unique_job_id_allowance', 'UNIQUE(job_id, allowance_id)', 'Cannot have many job allowance rel with exactly the same job and the same allowance')
] //This would cause an exception when you will be trying to save a job form that contains
many job.allowance.rel with the same allowance selected for the current job
Hope this would help 
Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 3 25
27687
2
thg 3 16
29513
0
thg 2 19
4163
naked domain set up Đã xử lý
3
thg 7 25
4139
0
thg 5 25
835