Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django中的继承:子类使用空字段更新父类_Python_Django_Python 3.x_Inheritance_Django Models - Fatal编程技术网

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

Python Django中的继承:子类使用空字段更新父类,python,django,python-3.x,inheritance,django-models,Python,Django,Python 3.x,Inheritance,Django Models,我正在为使用django.contrib.auth创建的每个用户创建一个UserProfile,方法是扩展基本抽象用户,并在注册新用户时捕获信号(我更喜欢为配置文件创建一个单独的表): 问题是,每次我创建一个用户时,基本用户都会首先正确插入,但创建概要文件会促使Django“更新”刚刚创建的基本用户(使用空值): DEBUG (0.001) INSERT INTO "auth_user" ("password", "last_login", "is_superuser", "username",

我正在为使用django.contrib.auth创建的每个用户创建一个UserProfile,方法是扩展基本抽象用户,并在注册新用户时捕获信号(我更喜欢为配置文件创建一个单独的表):

问题是,每次我创建一个用户时,基本用户都会首先正确插入,但创建概要文件会促使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总是这样更新模型,还是我遗漏了什么


Environment:Python 3.4.2+Django 1.7.4+PostgreSQL)

我怀疑问题在于您继承了
auth\u模型。User
。这是多表继承,不适合这种情况。请参见此处的django文档:

基本上就是说,如果您想将概要文件数据存储在一个单独的表中,则无需扩展
auth\u models.User
。只需使用
OneToOneField
创建一个模型,以
验证模型。用户

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)

我看到您正在使用自定义管理器
UserProfileManager
。你实现了你自己的
create
方法吗?@rojoca:没有,我没有在我的定制管理器中实现create方法,但我没想到Django会表现出这种行为。。。这是继承方面的正常行为吗?我知道还有其他几种方法来处理这个问题,但仍然让我困惑的是Django处理继承的方式。如果我已经提供了一个指向已创建和初始化的父对象的指针,那么在创建子对象后用空字段更新基本模型的原因是什么?@Ricardo这是因为当您调用
create
时,它会创建一个“新”的UserProfile模型。您传入的用户只需在新型号上设置
OneToOneField
。新的UserProfile模型将不会设置任何用户属性,除非您显式地这样做。保存后,这些用户属性都设置为空。
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)