我试图根据以下内容扩展django-registration
寄存器表单:
Python/Django django-registration add an extra field
但我得到了:
current transaction is aborted, commands ignored until end of transaction block
在调试过程中,我在建议的regbackend.py
中添加了一个断点,这表明损坏的代码位于:
from crewcal.models import UserProfile
from forms import *
def user_created(sender, user, request, **kwargs):
form = CustomRegistrationForm(request.POST)
data = UserProfile(user=user)
import ipdb; ipdb.set_trace();
data.locality = form.data["locality"]
data.save()
from registration.signals import user_registered
user_registered.connect(user_created)
问题(如下)可能与我的models.py
中定义的创建用户配置文件的方式有关:
在上面列出的regbackend.py
断点产生的shell中,我可以生成:
> /Users/project/app/regbackend.py(8)user_created()
7 import ipdb; ipdb.set_trace();
----> 8 data.locality = form.data["locality"]
9 data.save()
ipdb> data
<UserProfile: gub>
ipdb> vars(data)
{'user_id': 81, 'locality': None, '_user_cache': <User: gub>, '_state': <django.db.models.base.ModelState object at 0x103eb6990>, 'receive_email': True, 'id': None}
ipdb> form.data['locality']
u'BERLIN'
ipdb> data.locality = form.data['locality']
ipdb> vars(data)
{'user_id': 81, 'locality': u'BERLIN', '_user_cache': <User: gub>, '_state': <django.db.models.base.ModelState object at 0x103eb6990>, 'receive_email': True, 'id': None}
ipdb> data.save()
DEBUG (0.001) INSERT INTO "crewcal_userprofile" ("user_id", "receive_email", "locality") VALUES (81, true, 'BERLIN') RETURNING "crewcal_userprofile"."id"; args=(81, True, u'BERLIN')
*** InternalError: current transaction is aborted, commands ignored until end of transaction block
ipdb>
在详细的跟踪(包括sql)中,我得到:
[16/May/2014 07:53:50] "GET /register/ HTTP/1.1" 200 163203
DEBUG (0.003) SELECT (1) AS "a" FROM "auth_user" WHERE UPPER("auth_user"."username"::text) = UPPER('gub') LIMIT 1; args=(u'gub',)
DEBUG (0.001) SELECT "django_site"."id", "django_site"."domain", "django_site"."name" FROM "django_site" WHERE "django_site"."id" = 1 ; args=(1,)
DEBUG (0.001) INSERT INTO "auth_user" ("username", "first_name", "last_name", "email", "password", "is_staff", "is_active", "is_superuser", "last_login", "date_joined") VALUES ('gub', '', '', 'a@a.com', 'pbkdf2_sha256$10000$E2ZiaXLRtm0k$WrmqtRAhayt8w24Jpc8FYLTwRMbzDZIWhro/n/+hLpw=', false, true, false, '2014-05-16 07:54:00.398831', '2014-05-16 07:54:00.398831') RETURNING "auth_user"."id"; args=(u'gub', '', '', u'a@a.com', 'pbkdf2_sha256$10000$E2ZiaXLRtm0k$WrmqtRAhayt8w24Jpc8FYLTwRMbzDZIWhro/n/+hLpw=', False, True, False, u'2014-05-16 07:54:00.398831', u'2014-05-16 07:54:00.398831')
DEBUG (0.001) INSERT INTO "crewcal_userprofile" ("user_id", "receive_email", "locality") VALUES (81, true, NULL) RETURNING "crewcal_userprofile"."id"; args=(81, True, None)
DEBUG (0.001) INSERT INTO "crewcal_mycustomprofile" ("about_me", "facebook_id", "access_token", "facebook_name", "facebook_profile_url", "website_url", "blog_url", "date_of_birth", "gender", "raw_data", "image", "user_id") VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '', 81) RETURNING "crewcal_mycustomprofile"."id"; args=(None, None, None, None, None, None, None, None, None, None, u'', 81)
DEBUG (0.001) SELECT (1) AS "a" FROM "auth_user" WHERE "auth_user"."id" = 81 LIMIT 1; args=(81,)
DEBUG (0.002) UPDATE "auth_user" SET "username" = 'gub', "first_name" = '', "last_name" = '', "email" = 'a@a.com', "password" = 'pbkdf2_sha256$10000$E2ZiaXLRtm0k$WrmqtRAhayt8w24Jpc8FYLTwRMbzDZIWhro/n/+hLpw=', "is_staff" = false, "is_active" = false, "is_superuser" = false, "last_login" = '2014-05-16 07:54:00.398831', "date_joined" = '2014-05-16 07:54:00.398831' WHERE "auth_user"."id" = 81 ; args=(u'gub', '', '', u'a@a.com', 'pbkdf2_sha256$10000$E2ZiaXLRtm0k$WrmqtRAhayt8w24Jpc8FYLTwRMbzDZIWhro/n/+hLpw=', False, False, False, u'2014-05-16 07:54:00.398831', u'2014-05-16 07:54:00.398831', 81)
DEBUG (0.001) INSERT INTO "registration_registrationprofile" ("user_id", "activation_key") VALUES (81, 'f4ace49b34e503f271f252cb317bfbcc86be2238') RETURNING "registration_registrationprofile"."id"; args=(81, 'f4ace49b34e503f271f252cb317bfbcc86be2238')
我已经尝试将这些命令分别输入dbshell,但我看不出问题所在。在
有什么想法吗?在
当我遇到添加特殊字段或创建用户时执行任何特殊操作时,我宁愿避免覆盖
User
模型,并执行以下操作:Profile
,并向用户提供一个OneToOneField剖面模型示例
管理员py示例
^{pr2}$创建用户并为其添加个人资料
现在,您将把这个概要模型附加到每个用户,您可以添加您希望分析模型的字段,例如,如果您使:
user = User.objects.get(id=1)
您可以通过以下操作访问他的个人资料:
user.profile
调用任何函数
user.profile.function_name
您还可以获取配置文件并执行
profile.user
我知道您试图重写
User
模型,但我很肯定这是一种不太复杂和易于管理的方法,可以添加新的字段、操作或任何您需要的东西相关问题 更多 >
编程相关推荐