python - Django中的继承: child class updates parent with empty fields

标签 python django python-3.x inheritance django-models

我正在为使用 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。只需创建一个带有 OneToOneFieldauth_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/

相关文章:

Django 和 Gunicorn 无法通过 systemd 正常工作

python - 有没有办法在 Django 中删除目录并删除媒体文件?

python - 如何控制 django REST 框架返回的 JSON 的顺序?

python - 圆素数计划欧拉#35

python - 在 Python 中有效地解析 JSON 输出?

python - 在数据框中添加字典作为新行

python - 如何检测图像中的纸板箱?

python - 如何获取区间之外的值 pandas DataFrame

python - 如何将 numpy NaN 对象转换为 SQL 空值?

python - 何时以及如何在 python 中使用内置函数 property()