Skip to Content
Menu
This question has been flagged

Hello,

I need to create a certain model that has these fields among others:

- employee_id (self explaining)

- year (which corresponds to a year between the date employee_id's contract started and now)

I need to be able to select a year within said range, but I can't use Selection because it can't be populated via onchange, and using Many2one would require creating a whole model that only contains the field 'year', plus populating the new model's table with an arbitrary range, which doesn't make a lot of sense.

Is there any other way to achieve what I want?

Thanks a lot.

Avatar
Discard

need more explanation ....

Selection can be populated onchange. there are many examples in source code.

Best Answer

Hi,

If i had to do it, i would do what you suggest :

- create a simple year model that contains 1 Integer field year

- populate it with relevant range, e.g. with a csv file or with xml data

- add a many2one field to year_id in your model

- then add a dynamic domain on the Many2one field set with a 'onchange' depending of the employee_id e.g :

@api.onchange('employee_id')
def _onchange_employee_id(self):
if self.employee_id:
    your_domain = []
    contract_year = ... get contract year from employee
    your_domain += [('year','>', contract_year)] ... or whatever you need
    domain = {'year_id': [
            your_domain
    ]}

        return {'domain': domain}



Avatar
Discard

and then don't forget to add access rules to this simple model... it is true that is a bit heavy. If there is better solution, i'd also be curious to know !

I would also use widget='selection' in the view for the field year_id, to avoid creation/modification

Related Posts Replies Views Activity
1
Sep 23
2786
1
Feb 22
1787
1
Nov 21
1401
3
Mar 15
4817
2
Mar 24
1491