I’m trying to update the database if an entry is already there if not create a new one.
def saveprofile(request):
location = request.POST['location']
email = request.POST['email']
if request.user.is_authenticated():
userprofile = UserProfiles(user=request.user)
if userprofile:
userprofile.location=location
userprofile.email=email
userprofile.save()
return render_to_response('profile.html',{'pfields':userprofile})
else:
userprofile = UserProfiles(user=request.user, location=location, email=email)
userprofile.save()
return render_to_response('profile.html',{'pfields':userprofile})
It’s throwing
(1062, “Duplicate entry ’15’ for key ‘user_id'”)
You’ve got to use
getfor Django to fetch an existing object instead of creating a new one, which is what your call toUserProfiles(user=request.user)is currently doing.For example:
See this link for more information.