Se rendre au contenu
Menu
Cette question a été signalée
3 Réponses
620 Vues

I have this code for the resignation module I am working on. I need some specific options in the dropdown menu to be visible only to the female employees and the male employees cannot see them. This is for odoo v17.

But with this code, the reasons are not visible in the form view, it is just showing blank. Please help me with this.



reasons = fields.Selection(selection="_get_reasons", string='Reason', required=True)


    @api.onchange('gender')

    def _onchange_gender(self):

        # Update the resignation reasons based on the selected gender

        return {'domain': {'reasons': [('gender', '=', self.gender)]}}


    @api.model

    def _get_reasons(self):

        # Define resignation reasons based on the selected gender

        options = []

        if self.gender == 'male':

            options = [('reason1', 'Reason 1'),

                       ('reason2', 'Reason 2'),

                       ('reason4', "Reason 4"),

                       ('reason5', 'Reason 5'),

                       ('reason6', 'Reason 6'),

                       ('reason7', 'Reason 7'),

                       ('reason8', 'Reason 8')]

        elif self.gender == 'female':

            options = [('reason1', 'Reason 1'),

                       ('reason2', 'Reason 2'),

                       ('reason3', 'Reason 3'),

                       ('reason4', "Reason 4"),

                       ('reason5', 'Reason 5'),

                       ('reason6', 'Reason 6'),

                       ('reason7', 'Reason 7'),

                       ('reason8', 'Reason 8')]

        return options

Avatar
Ignorer
Meilleure réponse

Hey Frahh, 

Try the below code!

 
class Resignation(models.Model): 

​_name = 'your_module.resignation'

​_description = 'Resignation'

name = fields.Char(string='Employee Name', required=True) 

gender = fields.Selection([ 

​('male', 'Male'), 

​('female', 'Female'), 

​('other', 'Other'), 

​], string='Gender', required=True) 

reason = fields.Selection([ 

​('personal', 'Personal Reasons'), 

​('career', 'Career Change'), 

​('other', 'Other'), # Add more options as needed

], string='Resignation Reason', domain="[('gender', '=', 'female')]") 

@api.onchange('gender') def _onchange_gender(self): 

​if self.gender == 'male': 

​self.reason = False

Resignation()

Avatar
Ignorer
Meilleure réponse

Hello, 

kindly, try this:

def def _onchange_gender(self):
​some text

reasons = fields.Selection(selection=_get_reasons, string='Reason', required=True)
Avatar
Ignorer
Meilleure réponse

Hi frahh:

Remove the double quotes " around  _get_reasons in your field declaration like so and it should work.

reasons = fields.Selection(selection=_get_reasons, string='Reason', required=True)

Avatar
Ignorer
Auteur

Hello Paresh,
I have removed the double quotes but still, it is not working.