如何防止重复记录并只更新它?
Posted
技术标签:
【中文标题】如何防止重复记录并只更新它?【英文标题】:How to prevent duplicated records and only update it? 【发布时间】:2015-04-08 01:07:22 【问题描述】:我想在不重复的情况下将一些记录添加到另一个表模型中
我创建了一个函数来检查表数据并返回特定值以将其添加到另一个表中
这是我的代码
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
我的函数工作完美,但是当我向表中添加另一个值并尝试添加到它重复的其他字段时,我只想更新已经存在的日期,如果 excist 我尝试像这样更改我的函数但它没有工作:
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
我希望你得到我想要的:)
【问题讨论】:
【参考方案1】:您的意思是您正在尝试合并 2 个列表,但希望每个项目只有 1 个唯一实例?
如果是这种情况,您可以将所有数据添加到列表中,然后运行类似noobs_data_trimmed = list(set(noobs_data))
将列表放入集合中将消除集合中的完全相同的重复项。然后您可以将其转回列表以便于处理。
【讨论】:
不,你不明白我需要做什么,你是 odoo 开发者吗? 不,抱歉,只是 python。我想我明白你现在想要做什么。您正在尝试将 B 的记录字段中的值添加到相同记录 A 的字段值中。正确吗? 是的,如果字段是,则仅更新 B 中的一个字段。已经存在以上是关于如何防止重复记录并只更新它?的主要内容,如果未能解决你的问题,请参考以下文章