Question

I ve been following Dmitry tutorial to extend django-registration plugins registration with an additional field. Users can be registered now but additional field is not saved. I keep getting the error.

Django Version:1.4.2
Exception Type: InternalError Exception Value: current transaction is aborted, commands ignored until end of transaction block Exception
Location: /root/env27/lib/python2.7/site-packages/django/db/models/sql/compiler.py in execute_sql, line 910

Also looking at the postgresql logs, i see the following entry when i execute the register function.

required" value="serkan" name="name" />', NULL, NULL, NULL, NULL, NULL, NULL, NULL, E'') RETURNING "dirapp_userprofile"."id" 2013-03-03

12:28:45 EST ERROR: current transaction is aborted, commands ignored until end of transaction block 2013-03-03 12:28:45 EST STATEMENT: SHOW default_transaction_isolation 2013-03-03 12:29:59 EST ERROR: duplicate key value violates unique constraint "dirapp_userprofile_user_id_key" 2013-03-03 12:29:59 EST STATEMENT: INSERT INTO "dirapp_userprofile" ("user_id", "name", "email", "phone", "point", "address", "city", "state", "zipcode", "description") VALUES (48, E'serkan', NULL, NULL, NULL, NULL, NULL, NULL, NULL, E'') RETURNING "dirapp_userprofile"."id" 2013-03-03 12:29:59 EST ERROR: current transaction is aborted, commands ignored until end of transaction block 2013-03-03 12:29:59 EST STATEMENT: SHOW default_transaction_isolation ransaction_isolation 2013-03-03 12:29:59 EST ERROR: duplicate key value violates unique constraint "dirapp_userprofile_user_id_key" ransaction_isolation 2013-03-03 12:29:59 EST ERROR: duplicate key value violates unique constraint "dirapp_userprofile_user_id_key" 2013-03-03 12:36:59 EST ERROR: duplicate key value violates unique constraint "dirapp_userprofile_user_id_key" 2013-03-03 12:36:59 EST STATEMENT: INSERT INTO "dirapp_userprofile" ("user_id", "name", "email", "phone", "point", "address", "city", "state", "zipcode", "description") VALUES (49, E'serkan', NULL, NULL, NULL, NULL, NULL, NULL, NULL, E'') RETURNING "dirapp_userprofile"."id" 2013-03-03 12:36:59 EST ERROR: current transaction is aborted, commands ignored until end of transaction block 2013-03-03 12:36:59 EST STATEMENT: SHOW default_transaction_isolation 2013-03-03 13:02:10 EST ERROR: duplicate key value violates unique constraint "dirapp_userprofile_user_id_key" 2013-03-03 13:02:10 EST STATEMENT: INSERT INTO "dirapp_userprofile" ("user_id", "name", "email", "phone", "point", "address", "city", "state", "zipcode", "description") VALUES (50, E'serkan', NULL, NULL, NULL, NULL, NULL, NULL, NULL, E'') RETURNING "dirapp_userprofile"."id" 2013-03-03 13:02:10 EST ERROR: current transaction is aborted, commands ignored until end of transaction block 2013-03-03 13:02:10 EST STATEMENT: SHOW default_transaction_isolation 2013-03-03 13:54:51 EST ERROR: duplicate key value violates unique constraint "dirapp_userprofile_user_id_key" 2013-03-03 13:54:51 EST STATEMENT: INSERT INTO "dirapp_userprofile" ("user_id", "name", "email", "phone", "point", "address", "city", "state", "zipcode", "description") VALUES (51, E'serkan', NULL, NULL, NULL, NULL, NULL, NULL, NULL, E'') RETURNING "dirapp_userprofile"."id" 2013-03-03 13:54:51 EST ERROR: current transaction is aborted, commands ignored until end of transaction block 2013-03-03 13:54:51 EST STATEMENT: SHOW default_transaction_isolation

Cant figure out what might be problem, hoping someone can spot me to right direction.

models.py

class UserProfile(models.Model):

user = models.ForeignKey(User, unique=True)
name = models.CharField(max_length=60, blank=True, null=True)
email = models.CharField(max_length=50,blank=True, null=True)
phone = models.CharField(max_length=20, blank=True,null=True) 
point = models.PointField(srid=settings.SRID, blank=True, null=True)
address = models.CharField(max_length=60, blank=True, null=True)
city = models.CharField(max_length=60, blank=True, null=True)
state = models.CharField(max_length=60, blank=True, null=True)
zipcode = models.CharField(max_length=5,blank=True, null=True)
description = models.TextField()

def __unicode__(self):
    return unicode(self.user)

#Chcke whether the vendor box is checked @property def is_vendor(self): try: self.vendor return True except Vendor.DoesNotExist: return False

class UserProfileForm(ModelForm): class Meta: model = UserProfile exclude = ["user","point"]

urls.py

from djangoratings.views import AddRatingFromModel
from registration.views import register, activate
import dirapp.regbackend
from django.views.generic.simple import direct_to_template
from dirapp.forms import UserRegistrationFormz
from registration.views import register
#import registration.backends.default.urls as regUrls
import registration.backends.default.urls as regUrls


listing_list = {"queryset":Listing.objects.all()}


urlpatterns = patterns('', 
    url(r'^$',index,name='index'),
    url(r'^accounts/register/$', register, { 'backend': 'registration.backends.default.DefaultBackend','form_class':UserRegistrationFormz}, name='registration_register'),  
    url(r'^accounts/', include(regUrls)), 
    url(r'^profile/$', user_profile, name='user-profile-view'),

regbackend.py

    from dirapp.forms import UserRegistrationFormz
    from dirapp.models import UserProfile
    from django import forms

    def user_created(sender, user, request, **kwargs):
        form = UserRegistrationFormz(request.POST)
        data = UserProfile(user=user)
        data.name = form.data['name']
        data.save()

    from registration.signals import user_registered
    user_registered.connect(user_created)

forms.py

from django.contrib.gis import forms
from django.contrib.auth.models import User
from django.db.models import Q
from dirapp.models import UserProfile
from django import forms
from registration.forms import RegistrationForm, RegistrationFormTermsOfService
from django.forms import ModelForm
from django.utils.translation import ugettext_lazy as _
from registration.models import RegistrationProfile
from forms import *
from django import forms

#class ProfileForm(forms.Form):
#    name = forms.CharField()

#    def save(self,user):
#        try:
#            data = user.get_profile()
#        except:
#            data = UserProfile(user=user)
#        data.name = self.cleaned["name"]
#        data.save()



attrs_dict = {'class':'required'}

class UserRegistrationFormz(RegistrationForm):
    name = forms.CharField(widget=forms.TextInput(attrs=attrs_dict))

traceback

Environment:


Request Method: POST
Request URL: http://localhost:8000/accounts/register/

Django Version: 1.4.2
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.humanize',
 'django.contrib.gis',
 'django.contrib.staticfiles',
 'south',
 'dirapp',
 'registration',
 'django_extensions',
 'sorl.thumbnail',
 'debug_toolbar',
 'taggit',
 'djangoratings')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware')


Traceback:
File "/root/env27/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/root/posguide/../posguide/registration/views.py" in register
  187.             new_user = backend.register(request, **form.cleaned_data)
File "/root/posguide/registration/backends/default/__init__.py" in register
  82.                                      request=request)
File "/root/env27/lib/python2.7/site-packages/django/dispatch/dispatcher.py" in send
  172.             response = receiver(signal=self, sender=sender, **named)
File "/root/posguide/../posguide/dirapp/regbackend.py" in user_created
  9.     data.save()
File "/root/env27/lib/python2.7/site-packages/django/db/models/base.py" in save
  463.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/root/env27/lib/python2.7/site-packages/django/db/models/base.py" in save_base
  551.                 result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/root/env27/lib/python2.7/site-packages/django/db/models/manager.py" in _insert
  203.         return insert_query(self.model, objs, fields, **kwargs)
File "/root/env27/lib/python2.7/site-packages/django/db/models/query.py" in insert_query
  1593.     return query.get_compiler(using=using).execute_sql(return_id)
File "/root/env27/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  910.             cursor.execute(sql, params)

Exception Type: InternalError at /accounts/register/
Exception Value: current transaction is aborted, commands ignored until end of transaction block
Was it helpful?

Solution

Turns out it was trivial to solve. Slight modification to in query in regbackendpy got it working.

from dirapp.forms import UserRegistrationFormz
    from dirapp.models import UserProfile
    from django import forms

    def user_created(sender, user, request, **kwargs):
        form = UserRegistrationFormz(request.POST)
        **data = UserProfile.objects.get(user=user.id)**
        data.name = form.data['name']
        data.save()

    from registration.signals import user_registered
    user_registered.connect(user_created)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top