python - Django models create data duplicate -
1.models's name userrecord.
2.below code of view.
@login_required def data(request, page, keyword,strencode): current_username = request.user.username data_s = dosomething() #it takes long time!!! userrecord.objects.get_or_create(user=user.objects.get(username=current_username),myrecords=keyword) # in order create unique value # or use below method # if not userrecord.objects.filter(user=user.objects.get(username=current_username),myrecords=keyword): # userrecord.objects.create(user=user.objects.get(username=current_username),myrecords=keyword) return jsonresponse(data_s, safe=false)
requested below url several times no interval,something concurrent threading .
http://127.0.0.1:8000/data/1/test/english/
after operation done,myrecords column populated duplicate values.
found in django document use 'with transaction.atomic' deal problem did not work.
you don't need user user.objects.get(user=user.objects.get(username=current_username)) instead use request.user same.
@login_required def data(request, page, keyword,strencode): current_user = request.user data_s = dosomething() userrecord.objects.get_or_create(user=current_user,myrecords=keyword) # in order create unique value return jsonresponse(data_s, safe=false)
and myrecords=keyword create new record every time new keyword passed in def data view, please review code.
Comments
Post a Comment