我正在为使用 django.contrib.auth 创建的每个用户创建一个 UserProfile,方法是扩展基本抽象用户并在新用户注册时捕获信号(我更喜欢为配置文件创建一个单独的表):
class UserProfile(auth_models.User):
objects = UserProfileManager()
organization = models.CharField(max_length=100)
country = CountryField()
is_verified = models.BooleanField(default=False)
blocked = models.BooleanField(default=False)
anonymous = models.BooleanField(default=False)
@django_dispatch.receiver(
django_signals.post_save, sender=auth_models.User
)
def user_migrated(sender, instance, created, raw, **kwargs):
if not created or raw:
return
if instance.pk != 1:
return
account_models.UserProfile.objects.create(
user_ptr=instance,
organization='The SATNet Network',
country='US'
)
问题是,每次我创建用户时,都会首先正确插入基本用户,但配置文件的创建会促使 Django “更新”刚刚使用空值创建的基本用户:
DEBUG (0.001) INSERT INTO "auth_user" ("password", "last_login", "is_superuser", "username", "first_name", "last_name", "email", "is_staff", "is_active", "date_joined") VALUES ('pbkdf2_sha256$15000$TacxtITENWvn$e6LbDUtE1XcAwz/Kfj6vNj40yQurEOcBCRJe6LrBTls=', '2015-06-02 18:48:32.826604+00:00', true, 'satnet_admin', '', '', 'satnet.calpoly@gmail.com', true, true, '2015-06-02 18:48:32.826604+00:00') RETURNING "auth_user"."id"; args=('pbkdf2_sha256$15000$TacxtITENWvn$e6LbDUtE1XcAwz/Kfj6vNj40yQurEOcBCRJe6LrBTls=', '2015-06-02 18:48:32.826604+00:00', True, 'satnet_admin', '', '', 'satnet.calpoly@gmail.com', True, True, '2015-06-02 18:48:32.826604+00:00')
DEBUG (0.001) UPDATE "auth_user" SET "password" = '', "last_login" = '2015-06-02 18:48:32.886904+00:00', "is_superuser" = false, "username" = '', "first_name" = '', "last_name" = '', "email" = '', "is_staff" = false, "is_active" = true, "date_joined" = '2015-06-02 18:48:32.886951+00:00' WHERE "auth_user"."id" = 1; args=('', '2015-06-02 18:48:32.886904+00:00', False, '', '', '', '', False, True, '2015-06-02 18:48:32.886951+00:00', 1)
DEBUG (0.000) INSERT INTO "accounts_userprofile" ("user_ptr_id", "organization", "country", "is_verified", "blocked", "anonymous") VALUES (1, 'The SATNet Network', 'US', false, false, false); args=(1, 'The SATNet Network', 'US', False, False, False)
Superuser created successfully.
如果我不创建配置文件,则用户会被正确插入,因为 Django 不会更新模型:
DEBUG (0.001) INSERT INTO "auth_user" ("password", "last_login", "is_superuser", "username", "first_name", "last_name", "email", "is_staff", "is_active", "date_joined") VALUES ('pbkdf2_sha256$15000$nxWaMTdYVVKO$MGWYHw/NXoEwCxBryK+bYOoqTYsO0DXgyqkBEQNxq/I=', '2015-06-02 18:53:54.716366+00:00', true, 'satnet_admin', '', '', 'satnet.calpoly@gmail.com', true, true, '2015-06-02 18:53:54.716366+00:00') RETURNING "auth_user"."id"; args=('pbkdf2_sha256$15000$nxWaMTdYVVKO$MGWYHw/NXoEwCxBryK+bYOoqTYsO0DXgyqkBEQNxq/I=', '2015-06-02 18:53:54.716366+00:00', True, 'satnet_admin', '', '', 'satnet.calpoly@gmail.com', True, True, '2015-06-02 18:53:54.716366+00:00')
Superuser created successfully.
处理这个问题的正确方法是什么? Django 是否总是像这样更新模型,还是我遗漏了一些东西?
(环境:Python 3.4.2 + Django 1.7.4 + PostgreSQL)
最佳答案
我怀疑问题是您继承自auth_models.User
。这是多表继承,并不适合这种情况。请参阅此处的 django 文档:https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#extending-django-s-default-user
这基本上是说,如果您想将配置文件数据存储在单独的表中,则无需扩展auth_models.User
。只需创建一个带有 OneToOneField
到 auth_models.User
的模型即可:
class UserProfile(models.Model):
objects = UserProfileManager()
organization = models.CharField(max_length=100)
country = CountryField()
is_verified = models.BooleanField(default=False)
blocked = models.BooleanField(default=False)
anonymous = models.BooleanField(default=False)
user = models.OneToOneField(auth_models.User)
关于python - Django中的继承: child class updates parent with empty fields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30604423/