无法在扩展的Django注册模块上保存额外字段

时间:2013-03-03 18:08:36

标签: django django-registration

我一直在关注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 ERROR:当前事务中止,命令被忽略   直至交易结束2013-03-03 12:28:45 EST声明:   SHOW default_transaction_isolation 2013-03-03 12:29:59 EST ERROR:   重复键值违反唯一约束   “dirapp_userprofile_user_id_key”2013-03-03 12:29:59 EST声明:   INSERT INTO“dirapp_userprofile”(“user_id”,“name”,“email”,“phone”,   “点”,“地址”,“城市”,“州”,“邮政编码”,“描述”)价值观   (48,E'serkan',NULL,NULL,NULL,NULL,NULL,NULL,NULL,E'')   返回“dirapp_userprofile”。“id”2013-03-03 12:29:59 EST ERROR:   当前事务被中止,命令被忽略直到结束   交易区块2013-03-03 12:29:59 EST语句:显示   default_transaction_isolation ransaction_isolation 2013-03-03 12:29:59   EST ERROR:重复键值违反唯一约束   “dirapp_userprofile_user_id_key”ransaction_isolation 2013-03-03   12:29:59 EST ERROR:重复键值违反了唯一约束   “dirapp_userprofile_user_id_key”2013-03-03 12:36:59 EST ERROR:   重复键值违反唯一约束   “dirapp_userprofile_user_id_key”2013-03-03 12:36:59 EST声明:   INSERT INTO“dirapp_userprofile”(“user_id”,“name”,“email”,“phone”,   “点”,“地址”,“城市”,“州”,“邮政编码”,“描述”)价值观   (49,E'serkan',NULL,NULL,NULL,NULL,NULL,NULL,NULL,E'')   返回“dirapp_userprofile”。“id”2013-03-03 12:36:59 EST ERROR:   当前事务被中止,命令被忽略直到结束   交易区块2013-03-03 12:36:59 EST语句:显示   default_transaction_isolation 2013-03-03 13:02:10 EST错误:   重复键值违反唯一约束   “dirapp_userprofile_user_id_key”2013-03-03 13:02:10 EST声明:   INSERT INTO“dirapp_userprofile”(“user_id”,“name”,“email”,“phone”,   “点”,“地址”,“城市”,“州”,“邮政编码”,“描述”)价值观   (50,E'serkan',NULL,NULL,NULL,NULL,NULL,NULL,NULL,E'')   返回“dirapp_userprofile”。“id”2013-03-03 13:02:10 EST ERROR:   当前事务被中止,命令被忽略直到结束   交易块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声明:   INSERT INTO“dirapp_userprofile”(“user_id”,“name”,“email”,“phone”,   “点”,“地址”,“城市”,“州”,“邮政编码”,“描述”)价值观   (51,E'serkan',NULL,NULL,NULL,NULL,NULL,NULL,NULL,E'')   返回“dirapp_userprofile”。“id”2013-03-03 13:54:51 EST ERROR:   当前事务被中止,命令被忽略直到结束   交易区块2013-03-03 13:54:51 EST语句:显示   default_transaction_isolation

无法弄清楚可能存在什么问题,希望有人能指出我正确的方向。

models.py

  

类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是否检查供应商框@property def   is_vendor(self):try:self.vendor返回True,除了   Vendor.DoesNotExist:返回False

     

类UserProfileForm(ModelForm):       类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))

回溯

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

1 个答案:

答案 0 :(得分: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)