如何防止重复的记录而仅对其进行更新? [英] How to prevent duplicated records and only update it?

查看:75
本文介绍了如何防止重复的记录而仅对其进行更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些记录添加到另一个表模型中而不重复它

i want to add some records to another table model without duplicated it

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

i create a function to check the table data and return specific values to add it in another table

这是我的代码

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

我的函数可以正常工作,但是当我向表中添加另一个值并尝试将其添加到其他字段时,它重复了,但是我想只更新已存在的日期(如果存在),我尝试像这样更改函数,但没有工作:

my function work perfectly but when i add another value to my table and try to add to the other fields it duplicated but i want to just update the already date if excist i try to change my function like this but it didn't work :

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

我希望你得到了我想要的:)

I hope you got what i want :)

推荐答案

您是说要合并2个列表,但每个项目只具有1个唯一实例吗? 如果是这种情况,您可以将所有数据添加到列表中,然后运行类似noobs_data_trimmed = list(set(noobs_data))

Do you mean you're trying to merge 2 list but want to have only 1 unique instance of each item? If this is the case you could add all of the data to the list then run something like noobs_data_trimmed = list(set(noobs_data))

将列表放入集合中将消除集合中的确切重复项.然后,您可以将其重新转换为列表,以便于处理.

Making a list into a set will obliterate exact duplicates within the set. Then you can turn it back into a list for easier processing.

这篇关于如何防止重复的记录而仅对其进行更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆