Skip to Content
Menu
This question has been flagged
5 Replies
19175 Views

Class ExampleCode(models.Model) :

_name = 'example.code'

there is have a one event list

event_list = fields.Selection([
('commu', 'Community Event'),

('company', 'Company Event'),

('school'', 'School Event'),

('collage'', 'Collage Event'),

], string='Event Type')

there 4 set of option based on event_list :

position_company = fields.Selection([
                                                ('ceo', 'CEO'),
                                                ('manager', 'Manager'),
                                                ('employee', 'Employee'),
                                                ('other', 'Others')
                                                ], string='Position')

position_community = fields.Selection([
                                                ('leader', 'Leader'),
                                                ('vice', 'Vice Leader'),
                                                ('member', 'member'),
                                                ('speaker', 'Speaker'),
                                                ('other', 'Others')
                                                ], string='Position')

position_school = fields.Selection([
                                                ('head', 'Headmaster'),
                                                ('teacher', 'Teacher'),
                                                ('council', 'Student Council'),
                                                ('student', 'Student'),
                                                ('other', 'Others')
                                                ], string='Position')

position_collage = fields.Selection([
                                                ('rector', 'Rector'),
                                                ('lecture', 'Lecturer'),
                                                ('student', 'Collage Student),
                                                ('other', 'Others')
                                                ], string='Position')


so instead of making 4 set of them i wanna to put them all in one fields selection then it would be invisible based on event_list is that possible ? if possible can you show me how to do it ?


reason why i wanna do something like this because i dont wanna show 4 position on one tree view so i should make something like this if there is another alternative please tell me how to do this .


Thank you,

Best Regards

Avatar
Discard
Best Answer

As an alternative approach you can use a computed field that will have the correct selection, but still use the 4 sub selections to dynamically show the appropriate options:

from odoo import api, fields, models

COMPANY_SELECTION = [
('ceo', 'CEO'),
('manager', 'Manager'),
('employee', 'Employee'),
('other', 'Others')
]
COMMUNITY_SELECTION = [
('leader', 'Leader'),
('vice', 'Vice Leader'),
('member', 'member'),
('speaker', 'Speaker'),
('other', 'Others')
]
SCHOOL_SELECTION = [
('head', 'Headmaster'),
('teacher', 'Teacher'),
('council', 'Student Council'),
('student', 'Student'),
('other', 'Others')
]
COLLAGE_SELECTION = [
('rector', 'Rector'),
('lecture', 'Lecturer'),
('student', 'Collage Student'),
('other', 'Others')
]


class ExampleCode(models.Model):
_name = 'example.code'
event_list = fields.Selection(string='Event Type', selection=[
('company', 'Company Event'),
('commu', 'Community Event'),
('school', 'School Event'),
('collage', 'Collage Event')
])

position_company = fields.Selection(string='Company Position', selection=COMPANY_SELECTION)
position_community = fields.Selection(string='Company Position', selection=COMMUNITY_SELECTION)
position_school = fields.Selection(string='Company Position', selection=SCHOOL_SELECTION)
position_collage = fields.Selection(string='Company Position', selection=COLLAGE_SELECTION)
position = fields.Selection(string="Position", selection=COMPANY_SELECTION + COMMUNITY_SELECTION + SCHOOL_SELECTION + COLLAGE_SELECTION, compute='_compute_position', store=True)

@api.depends('position', 'position_company', 'position_community', 'position_school', 'position_collage')
def _compute_position(self):
for rec in self:
if rec.event_list == 'company':
rec.position = rec.position_company
elif rec.event_list == 'commu':
rec.position = rec.position_community
elif rec.event_list == 'school':
rec.position = rec.position_school
elif rec.event_list == 'collage':
rec.position = rec.position_collage
else:
                rec.position = False


Avatar
Discard
Best Answer

There is no way to filter value of selection field or even would not apply domain on it.

So the alternative is 
Instead of taking four selection field take one many2one field like
                                                                                                                                                                                                  
position_id = fields.Many2one('job.position')

in job.position take named as type as a selection field like [('community','Community'), ('company','Company'), ....]

now on change of event_list set domain on position_id like
return {'domain': {'position_id': [('type', '=', self.event_list)]}}

Avatar
Discard
Author

well i thinks is not a good way to solve this problem, because i must make the many2one to handle this things. and my system already full of selection field for this, so i must fix all of them if i using this way.

Ok, but my main concern about your approach is if you create four different selection field when you need position value in another part of business logic you have to check/select all the fields

Author

thanks for you answer, i'm already found the alternative that can dynamically to change fields selection based on condition, it just need the 4 global variable and 1 selection field and return the domain for filtering the slection field without change to m2o

@Chaanto Please Share your Answer ..

Related Posts Replies Views Activity
2
Dec 17
16680
0
Nov 17
5505
0
Apr 15
3815
1
Mar 15
9365
1
Dec 24
1392