Python 如何防止重复记录并仅对其进行更新?

Python 如何防止重复记录并仅对其进行更新?,python,orm,openerp,odoo,openerp-8,Python,Orm,Openerp,Odoo,Openerp 8,我想向另一个表模型添加一些记录,而不复制它 我创建了一个函数来检查表数据,并返回特定值以将其添加到另一个表中 这是我的密码 def lol_hah(self,cr,uid,ids,context=None): noobs_data=[] cr.execute("select DISTINCT ON (subject_id)subject_id from fci_attendance_line") noobs1 = cr.dictfetchall()

我想向另一个表模型添加一些记录,而不复制它

我创建了一个函数来检查表数据,并返回特定值以将其添加到另一个表中

这是我的密码

def lol_hah(self,cr,uid,ids,context=None):
        noobs_data=[]
        cr.execute("select DISTINCT ON (subject_id)subject_id from fci_attendance_line")
        noobs1 = cr.dictfetchall()
        ages = [li['subject_id'] for li in noobs1]
        print (ages)
        for k in ages:
            cr.execute(
            "select DISTINCT ON (student_id)student_id, count(present) AS Number_of_Absenece,present,subject_id as subject_name, s.name  AS Student_Name,s.standard_id,s.group_id  from fci_attendance_line ,fci_student s where subject_id=%d and present=False and  s.id=student_id group by student_id ,s.sit_number,present, s.name,s.standard_id,s.group_id ,subject_id "% (
                k))
            noobs = cr.dictfetchall()
            cr.execute(
            "select DISTINCT ON (student_id)student_id, count(present) AS Number_of_Absenece,present,subject_id as subject_name, s.name  AS Student_Name,s.standard_id,s.group_id  from fci_attendance_line ,fci_student s where subject_id=%d and present=False and  s.id=student_id group by student_id ,s.sit_number,present, s.name,s.standard_id,s.group_id ,subject_id "% (
                k))
            noobs_details = cr.dictfetchall()
            for details_ids in noobs_details:
                for data in noobs:
                    details_ids[data['student_id']] = str(data['number_of_absenece'])+str(data['student_id']) + str(data['standard_id'])+str(data['group_id'])+str(data['subject_name'])
                noobs_data.append(details_ids)
        print (noobs_data)
        subo_obj = self.pool.get('fci.attendance.subjects')
        count=0
        for name in noobs_data:
            count =count+1
            student_ids=self.search(cr,uid,[('student_id.id','=',int(name['student_id']))])
            if student_ids and int(name['number_of_absenece']) >= 3:
                subo_obj.create(cr, uid,{'student_id':int(name['student_id']),
                                                         'number_of_absence':int(name['number_of_absenece']),
                                                         'subject_id':int(name['subject_name']),
                                                         'standard_id':int(name['standard_id']),
                                                         'standard_group':int(name['group_id'])})
        print ('Number of times LOL : ',count)
        return True
我的函数工作正常,但当我向表中添加另一个值并尝试向其他字段添加时,它会重复,但我只想更新已存在的日期,如果我尝试像这样更改函数,但它不起作用:

def lol_hah(self,cr,uid,ids,context=None):
        noobs_data=[]
        cr.execute("select DISTINCT ON (subject_id)subject_id from fci_attendance_line")
        noobs1 = cr.dictfetchall()
        ages = [li['subject_id'] for li in noobs1]
        print (ages)
        for k in ages:
            cr.execute(
            "select DISTINCT ON (student_id)student_id, count(present) AS Number_of_Absenece,present,subject_id as subject_name, s.name  AS Student_Name,s.standard_id,s.group_id  from fci_attendance_line ,fci_student s where subject_id=%d and present=False and  s.id=student_id group by student_id ,s.sit_number,present, s.name,s.standard_id,s.group_id ,subject_id "% (
                k))
            noobs = cr.dictfetchall()
            cr.execute(
            "select DISTINCT ON (student_id)student_id, count(present) AS Number_of_Absenece,present,subject_id as subject_name, s.name  AS Student_Name,s.standard_id,s.group_id  from fci_attendance_line ,fci_student s where subject_id=%d and present=False and  s.id=student_id group by student_id ,s.sit_number,present, s.name,s.standard_id,s.group_id ,subject_id "% (
                k))
            noobs_details = cr.dictfetchall()
            for details_ids in noobs_details:
                for data in noobs:
                    details_ids[data['student_id']] = str(data['number_of_absenece'])+str(data['student_id']) + str(data['standard_id'])+str(data['group_id'])+str(data['subject_name'])
                noobs_data.append(details_ids)
        print (noobs_data)
        subo_obj = self.pool.get('fci.attendance.subjects')
        count=0
        for name in noobs_data:
            count =count+1
            student_ids=self.search(cr,uid,[('student_id.id','=',int(name['student_id']))])
            if student_ids and int(name['number_of_absenece']) >= 3:
                    ds_ids=subo_obj.search(cr,uid,[('student_id.id','=',int(name['student_id']))])
                    print('Here is ids found',ds_ids)
                    if ds_ids != []:
                        subo_obj.write(cr, uid, ds_ids, {'number_of_absence': int(name['number_of_absenece'])}, context=context)
                    else:
                        subo_obj.create(cr, uid,{'student_id':int(name['student_id']),
                                                         'number_of_absence':int(name['number_of_absenece']),
                                                         'subject_id':int(name['subject_name']),
                                                         'standard_id':int(name['standard_id']),
                                                         'standard_group':int(name['group_id'])})
        print ('Number of times LOL : ',count)
        return True

我希望您得到了我想要的:

您的意思是您试图合并两个列表,但希望每个项目只有一个唯一的实例吗? 如果是这种情况,您可以将所有数据添加到列表中,然后运行noobs\u data\u trimmed=listsetnoobs\u data之类的操作


将列表编入一个集合将删除集合中的完全重复项。然后您可以将其转换回列表以便于处理。

不,您不明白我需要做什么,您是odoo开发人员吗?不,抱歉,只是python。我想我明白你现在想做什么了。您试图将B记录字段中的值添加到相同记录A的字段值中。是否正确?如果字段为,则只更新B中的一个字段。已经激进分子