How to prevent duplicated records and only update it?

妖精的绣舞 提交于 2019-12-25 03:11:48

问题


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

here is my code

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 :)


回答1:


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.



来源:https://stackoverflow.com/questions/29504161/how-to-prevent-duplicated-records-and-only-update-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!