We use paypal on our system to check whether a user has paid before and already has an account.
This morning I received a traceback that basically gave me an integrity error.
IntegrityError: (1062, “Duplicate entry ‘user_1234_before’ for key 2”)
My statemtent looks as follows.
try:
user = User.objects.get(email=ipn_obj.payer_email)
except:
user_slug = ("%s_%s_before") % (ipn_obj.first_name, ipn_obj.last_name)
username = slugify(user_slug)
user = User.objects.create_user(username, ipn_obj.payer_email, 'testpassword')
user.first_name = ipn_obj.first_name
user.last_name = ipn_obj.last_name
user.save()
Thanks in advance.
Never, ever use a blank
exceptstatement. What’s happening here is an excellent demonstration of why.You’ve presumably used that try/except block to catch the
User.DoesNotExistexception. However, your code is actually raising a completely different exception. Because you’re swallowing it, it’s impossible to know which one, but potentiallyipn_objisn’t what you think it is and doesn’t have apayer_emailerror, so you’re gettingAttributeError. Or, possibly, you’re getting theUser.MultipleObjectReturnedexception.Change your
excepttoexcept User.DoesNotExist, and then debug your actual problem.