I want to keep an odoo form, with at least one field must be different and not keep a duplicate of the form

I created a form where I want to save all the information by filling it out, but the condition there should not be a duplicate record with the same records in all fields, at least one other record will work.

Here is my code -

class HRLeaveRules(models.Model):
    _name = 'hr_leave_rules.leave_rules'
    half_day_allowed = fields.Selection([
        ('yes',"Yes"),
        ('no',"No")],
        string="Half Day Allowed", required=True)
    gender_specific = fields.Selection([
        ('all' ,"All"),
        ('male',"Male"),
        ('female',"Female")],
        string="Gender Specific", required=True)    
    leaves_allowed_on_prorata_basis = fields.Selection([
        ('yes',"Yes"),
        ('no',"No")],
        string="Leaves allowed on pro rata basis", required=True)
    leave_encashment = fields.Boolean(string="Leave Encashment", 
        required=True)
    leave_encashment_for_maximum = fields.Integer(
        string = "Leave Encashment for maximum", required=True)     
    can_emp_club_leave = fields.Selection([
        ('yes',"Yes"),
        ('no',"No")],
        string="Can Employees Club this leave with any other leave", 
        required=True)  
    past_dated_leave_allowed = fields.Selection([
        ('yes',"Yes"),
        ('no',"No")],
        string="Past dated leave application allowed", required=True)   
    override_paid_leave_to_unpaid = fields.Selection([
        ('yes',"Yes"),
        ('no',"No")],
        string="Can managers override paid leaves to unpaid", required=True)    
    carry_frwrdng_leaves = fields.Boolean(string="Carry forwarding of leaves", 
        required=True)  
    maximum_accumulation_in_year = fields.Integer(string = "Maximum Accumulation in year", 
        required=True)  
    leave_encash_rest_leaves = fields.Selection([
        ('yes',"Yes"),
        ('no',"No")],
        string="Leave Encashment for Rest Leaves", required=True)
    employee_id = fields.Many2one('hr.employee', string="Employee")
    holiday_status_id = fields.Many2one("hr.holidays.status", 
        string="Leave Type", required=True) 
    department_id = fields.Many2one('hr.department', 
        related='employee_id.department_id', string='Department') 
    count_intervening_leaves = fields.Boolean(
        string="Count intervening holidays/weekly offs as leaves", 
        required=True)
    count_intervening_leaves_back_date = fields.Boolean(
        string="Count intervening holidays/weekly offs as leaves if applying for back date", 
        required=True)
    leaves_probation_period = fields.Boolean(string="Leaves allowed in probation period", 
        required=True)
    max_con_leaves_month = fields.Boolean(string="Maximum consecutive leaves per month", 
        required=True)
    leave_encashment_cycle = fields.Selection([
        ('annually',"Annually"),
        ('super_annuation',"Super Annuation / Relieving")],
        string="Leave Encashment Cycle", required=True)
    description = fields.Text(string="Description")

    @api.model
    def create(self,value):
        current = self.env['hr_leave_rules.leave_rules'].search([])
        for rec in current:
            if value.has_key("holiday_status_id"):
                if rec.holiday_status_id.id == value["holiday_status_id"] and rec.holiday_status_id:
                    if value.has_key("gender_specific"):
                        if rec.gender_specific == value["gender_specific"] and rec.gender_specific:                 
                            if value.has_key("half_day_allowed"):
                                if rec.half_day_allowed == value["half_day_allowed"] and rec.half_day_allowed:                  
                                    if value.has_key("leaves_allowed_on_prorata_basis"):
                                        if rec.leaves_allowed_on_prorata_basis == value["leaves_allowed_on_prorata_basis"] and rec.leaves_allowed_on_prorata_basis:                 
                                            if value.has_key("leave_encashment_cycle"):
                                                if rec.leave_encashment_cycle == value["leave_encashment_cycle"] and rec.leave_encashment_cycle:
                                                    if value.has_key("leave_encash_rest_leaves"):
                                                        if rec.leave_encash_rest_leaves == value["leave_encash_rest_leaves"] and rec.leave_encash_rest_leaves:                                                  
                                                            if value.has_key("can_emp_club_leave"):
                                                                if rec.can_emp_club_leave == value["can_emp_club_leave"] and rec.can_emp_club_leave:
                                                                    if value.has_key("past_dated_leave_allowed"):
                                                                        if rec.past_dated_leave_allowed == value["past_dated_leave_allowed"] and rec.past_dated_leave_allowed:
                                                                            if value.has_key("override_paid_leave_to_unpaid"):
                                                                                if rec.override_paid_leave_to_unpaid == value["override_paid_leave_to_unpaid"] and rec.override_paid_leave_to_unpaid:
                                                                                    raise UserError(_('This type of leave already exist in the leave rules !'))

      

The condition for the absence of a duplicate record works correctly, but the unique form does not store information

Mistake -

AttributeError: object "NoneType" has no attribute "id"

+3


source to share


1 answer


How about using sql constraint:



class MyModel(models.Model):
    _name = "my.model"

    field1 = fields.Char()
    field2 = fields.Char()
    field3 = fields.Char()

    _sql_constraints = [
        ('unique_my_model', 'unique(field1, field2, field3)',
         'Combination of all fields have to be unique!')
    ]

      

+3


source







All Articles