Django中的继承:子类使用空字段更新父类

时间:2015-06-02 19:00:12

标签: 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)

1 个答案:

答案 0 :(得分:2)

我怀疑问题是你是从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)