1
try: 
   user = User.objects.create_user(_username, _email, pwd)
except IntegrityError, e:
   fail = e.message
   return render_to_response('register.html',{'reg_fail':fail},context_instance=RequestContext(request)) 

I have this code, once i catch the IntegrityError, i am getting this error: current transaction is aborted, commands ignored until end of transaction block

why is this? if i delete the context_instance part, then i am getting again the page but without any media access. I am stuck, i want just to register a user or if integrityError, then render to register page with error message.

by the way: i am using django1.4 and postgresql. and User is django's auth user

doniyor
  • 36,596
  • 57
  • 175
  • 260

2 Answers2

1

With user_create you start transaction, and doing this again cause the error of not commited transaction.You have to commit(end) your transaction after user_create as follow:

try: 
   user = User.objects.create_user(_username, _email, pwd)
   user.full_clean()
   user.save()
except IntegrityError, e:
   fail = e.message
   return render_to_response('register.html',{'reg_fail':fail},context_instance=RequestContext(request))
adamr
  • 740
  • 6
  • 18
  • oh, i thought, ``create_user`` will save automatically, doesnot it do? – doniyor Aug 03 '13 at 19:46
  • No it don't. When you call create_user, you just create User object, full_clean is validating data and save is commit of transaction. – adamr Aug 03 '13 at 19:49
  • https://docs.djangoproject.com/en/1.4/topics/auth/#django.contrib.auth.models.UserManager.create_user look at here – doniyor Aug 03 '13 at 19:49
  • And as you can see there save() method is inserting data into db, user_create just create object. – adamr Aug 03 '13 at 19:51
  • not really, i see this sentence there: ``Creates, saves and returns a User.`` – doniyor Aug 03 '13 at 19:53
  • look here at save(): https://docs.djangoproject.com/en/dev/topics/db/queries/ "Django doesn’t hit the database until you explicitly call save()." – adamr Aug 03 '13 at 19:56
  • i know this, but not in my case, because ``create_user`` is a method of Django's User Model. see here https://docs.djangoproject.com/en/1.4/topics/auth/#creating-users – doniyor Aug 03 '13 at 19:58
  • Hmmm, you are right I just looked at source code of create_user and save method is called there. But everything works fine after adding save method call? – adamr Aug 03 '13 at 20:05
  • Try to make a rollback, someone got the same error with postgres:http://stackoverflow.com/questions/2979369/databaseerror-current-transaction-is-aborted-commands-ignored-until-end-of-tra – adamr Aug 03 '13 at 20:13
  • yeaah, i am trying now :D – doniyor Aug 03 '13 at 20:22
0

Are you using a default database or couple database. it's recommended to use db_manager() to specify the database. db_manager() returns a copy of the manager bound to the database you specify.

User.objects.db_manager(’new_users’).create_user(...)
drabo2005
  • 1,076
  • 1
  • 10
  • 16