内容简介:翻译自:https://stackoverflow.com/questions/23695713/django-reg-extend-current-transaction-is-aborted-commands-ignored-until-end-o
-registration寄存器表单:
Python/Django django-registration add an extra field
但我得到了:
当前事务被中止,命令被忽略直到事务块结束
在调试中,我在建议的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中定义的创建用户配置文件的方式有关:
def create_user_profile(sender, instance, created, raw, **kwargs): if created and not raw: print vars(instance) UserProfile.objects.create(user=instance) post_save.connect(create_user_profile, sender=User) User.profile = property(lambda u: UserProfile.\ objects.get_or_create(user=u)[0])
在上面列出的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,但我看不出问题.
有任何想法吗?
当我面对添加特殊字段或在创建用户时执行任何特殊操作时,我宁愿避免覆盖用户模型并执行以下操作:
>您创建一个新模型,例如使用OneToOneField的Profile
>将所需字段添加到该配置文件模型,例如(tlf,country,language,log …)
>在创建时添加任何特殊操作,例如保存日志,存储更多信息….
>在django admin中管理用户的同时,创建admin.py来管理此模型(配置文件)
配置模型示例
class Profile(models.Model): user = models.OneToOneField(User) phone = models.CharField(max_length=255, blank=True, null=True, verbose_name='phone') description = models.TextField(blank=True, verbose_name='descripction') ... ... def not_first_log(self): # Just a tiny example of a function to mark user as first-logged self.first_log = False self.save() class Meta: ordering = ['user'] verbose_name = 'user' verbose_name_plural = 'users'
admin.py示例
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.contrib import admin from django.contrib.auth.admin import UserAdmin from django.contrib.auth.models import User class ProfileInline(admin.StackedInline): model = Profile can_delete = False filter_horizontal = ['filter fields'] # example: ['tlf', 'country',...] verbose_name_plural = 'profiles' fk_name = 'user' class UserAdmin(UserAdmin): inlines = (ProfileInline, ) list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff') list_filter = ('is_staff', 'is_superuser', 'is_active') admin.site.unregister(User) # Unregister user to add new inline ProfileInline admin.site.register(User, UserAdmin) # Register User with this inline profile
创建用户并将配置文件附加到他
# Create user username = 'TestUser' email = 'test@example.com' passw = '1234' new_user = User.objects.create_user(username, email, passw) # Create profile phone = '654654654' desc = 'Test user profile' new_profile = Profile(user=new_user, phone = phone, description=desc) new_profile.profile_role = new_u_prole new_profile.user = new_user # Save profile and user new_profile.save() new_profile.not_first_log() new_user.save()
现在,您将为每个用户添加此配置文件模型,并且可以将您希望的字段添加到配置文件模型,例如,如果您创建:
user = User.objects.get(id = 1)
你可以访问他的个人资料:
用户资料
并调用任何功能
user.profile.function_name
您还可以获取配置文件并执行profile.user
我知道你试图覆盖用户模型,但我很确定这种方式不那么复杂,易于管理,添加新字段,操作或任何你需要的东西
翻译自:https://stackoverflow.com/questions/23695713/django-reg-extend-current-transaction-is-aborted-commands-ignored-until-end-o
以上所述就是小编给大家介绍的《python – django reg extend – 当前事务被中止,命令被忽略直到事务块结束》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 如何优雅地中止线程?
- Java多线程之线程中止
- Haskell程序中止与“循环”,但我认为它不应该
- Java事务解析(事务的基本操作+隔离的等级+事务的四大特性+事务的概念)
- 在云中管理事务,第 1 部分: 事务基础和分布式事务
- Spring事务专题(三)事务的基本概念,Mysql事务处理原理
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
运营有道:重新定义互联网运营
李明轩 / 机械工业出版社 / 2017-7-31 / 69.00元
本书是前百度资深运营专家多年运营经验的总结,是作者运营千万级用户规模的大型互联网产品的实操经验复盘,是作者在“在行”上为近百位CEO和高管提供互联网运营咨询服务后对互联网运营需求的深入洞见。 本书的思想基础是“运营必须以用户为中心”,从产品、用户、市场3个维度对互联网运营重新进行了系统性的梳理:从道的层面解读并重新定义运营方法论,从术的层面围绕方法论提出行之有效的解决方法和实际案例。重点不在......一起来看看 《运营有道:重新定义互联网运营》 这本书的介绍吧!