其他分享
首页 > 其他分享> > 我想保存至少一个字段的odoo表单应该是不同的,并且表单没有重复保存

我想保存至少一个字段的odoo表单应该是不同的,并且表单没有重复保存

作者:互联网

我创建了一个表单,希望通过填写该表单来存储所有信息,但条件是在所有字段中都不应有相同条目的重复条目,至少一个不同的条目将起作用.

这是我的代码-

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 !'))

没有重复输入的条件正常运行,但唯一表单未保存信息

错误是-

AttributeError: ‘NoneType’ object has no attribute ‘id’

解决方法:

如何使用sql约束:

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!')
    ]

标签:odoo-10,openerp,forms,python
来源: https://codeday.me/bug/20191111/2018041.html