0

我一直在关注Dmitry教程,以使用附加字段扩展 django-registration 插件注册。现在可以注册用户,但不保存附加字段。我不断收到错误。

Django 版本:1.4.2
异常类型:InternalError 异常值:当前事务被中止,命令被忽略,直到事务块结束异常
位置:/root/env27/lib/python2.7/site-packages/django/db/models/sql /compiler.py 在 execute_sql 中,第 910 行

还查看 postgresql 日志,当我执行注册函数时,我看到以下条目。

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

12:28:45 EST 错误:当前事务被中止,命令被忽略,直到事务块结束 2013-03-03 12:28:45 EST 声明:显示 default_transaction_isolation 2013-03-03 12:29:59 EST 错误:重复键值违反了唯一约束“dirapp_userprofile_user_id_key” 2013-03-03 12:29:59 EST 声明:插入到“dirapp_userprofile”(“user_id”、“name”、“email”、“phone”、“point”、“address”、 “城市”,“州”,“邮政编码”,“描述”)值(48,E'serkan',NULL,NULL,NULL,NULL,NULL,NULL,NULL,E'')返回“dirapp_userprofile”。“id " 2013-03-03 12:29:59 EST 错误:当前事务被中止,命令被忽略,直到事务块结束 2013-03-03 12:29:59 EST 声明:显示 default_transaction_isolation ransaction_isolation 2013-03-03 12:29:59 EST 错误:重复键值违反唯一约束“dirapp_userprofile_user_id_key”ransaction_isolation 2013-03- 03 12:29:59 EST 错误:重复键值违反了唯一约束“dirapp_userprofile_user_id_key” 2013-03-03 12:36:59 EST 错误:重复键值违反了唯一约束“dirapp_userprofile_user_id_key” 2013-03-03 12:36:59 EST 语句:插入“dirapp_userprofile”(“user_id”、“name”、“email”、“phone”、“point”、“address”、“city”、“state”、“zipcode”、“description”)值(49,E'serkan',NULL,NULL,NULL,NULL,NULL,NULL,NULL,E'')返回“dirapp_userprofile”。“id”2013-03-03 12:36:59 EST 错误:当前事务被中止,命令被忽略直到事务结束块 2013-03-03 12:36:59 EST STATEMENT:显示 default_transaction_isolation 2013-03-03 13:02:10 EST 错误:重复键值违反唯一约束“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'') 返回 "dirapp_userprofile"."id"2013-03-03 13:02:10 EST 错误:当前事务被中止,命令被忽略,直到事务块结束 2013-03-03 13:02:10 EST 声明:显示 default_transaction_isolation 2013-03-03 13:54:51 EST 错误:重复键值违反唯一约束“dirapp_userprofile_user_id_key” 2013-03-03 13:54:51 EST 声明:插入“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 错误:当前事务已中止,直到事务块结束 2013-03-03 13:54:51 EST STATEMENT: SHOW default_transaction_isolation

无法弄清楚可能是什么问题,希望有人能发现我正确的方向。

模型.py

类用户配置文件(模型。模型):

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)

#检查供应商框是否被选中 @property def is_vendor(self): try: self.vendor return True except Vendor.DoesNotExist: return False

类 UserProfileForm(ModelForm): 类 Meta: 模型 = UserProfile exclude = ["user","point"]

网址.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)

表格.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))

追溯

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
4

1 回答 1

0

事实证明解决起来很简单。对 regbackendpy 中的查询稍作修改即可使其正常工作。

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)
于 2013-03-04T02:00:24.893 回答