10434

How to prevent duplicated records and only update it?

Question:

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

Answer1:

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.

Recommend

  • how to access the label inside datatemplate
  • rails:how to create dynamic drop-down box in rails?
  • Php Variable in Cakephp Model
  • ResultTransformer in Hibernate return null
  • RpgSQL - Why is it so slow?
  • Jquery ajax form array submit
  • SQL Query (or Join) for 3 tables
  • PHP SQL database query error message
  • How to get count of people based on age groups using SQL query in Oracle database?
  • Closest value different files, with different number of lines and other conditions ( bash awk other)
  • Regex takes a long time to complete
  • sql1 to get names and sql2 to get Impressions, I need to sort names by Impressions
  • EntityDataSource query inner join
  • SQL how to do an outer join properly
  • SQL Count. How can I count how many distinct values are in a table when an other two columns are mat
  • SQL duplicate rows with multiple left joins
  • JPA - getting distinct value from one column
  • How to translate SQL queries to cypher in the optimal way?
  • Add delivery info to query in SAP Crystal Reports
  • Mapping ManyToMany with composite Primary key and Annotation:
  • Taking mean across rows grouped by a variable in numpy
  • Grouping by blank nodes
  • Query to get the Top 2 from each group
  • How to implement limit with Nhibernate and Sybase
  • Passing information to server-side function in a Google Docs Add On
  • What's the syntax to inherit documentation from another indexer?
  • Using Sax parsing to edit and write XML in VB6
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Django: Count of Group Elements
  • Using jQuery closest() method with class selector
  • Is there any way to access browser form field suggestions from JavaScript?
  • Sending data from AppleScript to FileMaker records
  • MySQL WHERE-condition in procedure ignored
  • jQuery tmpl and DataLink beta
  • Jquery - Jquery Wysiwyg return html as a string
  • align graphs with different xlab
  • Return words with double consecutive letters
  • Reading document lines to the user (python)
  • Observable and ngFor in Angular 2
  • Python/Django TangoWithDjango Models and Databases