接口实现删除重复的数据
def userdelete(request):
exit_user_to = User.objects.get(username=user_to)
obj = Gerrit_Reviewer_User.objects.filter(user_id=int(exit_user_to.id))
result = []
for i in obj:
sql1 = "select id,count(*) as count from gerrit_reviewer_user where user_id=%s and gtrevuser_id=%s and type_user ='%s'" % (
i.user_id,i.gtrevuser_id,i.type_user)
regular_obj = Gerrit_Reviewer_User.objects.raw(sql1)
for j in regular_obj:
if int(j.count)==1:
continue
elif int(j.count)==2:
result.append(j.id)
result_1 = list(set(result))
for k in result_1:
Gerrit_Reviewer_User.objects.get(id=int(k)).delete()
cursor.close()
db.close()
return JsonResponse({
'success': '修改成功'
})
删除的关键时user_id,gtrevuser_id,type_user 这三个字段的值要一样,查询出来的结果count为2默认会取第一条,拿到ID删除即可
sql1 = "select id,count(*) as count from gerrit_reviewer_user where user_id=%s and gtrevuser_id=%s and type_user ='%s'" % (i.user_id,i.gtrevuser_id,i.type_user)
|