在Django中更新一对多引用字段也会更新其他对象

时间:2014-09-24 06:42:28

标签: python django django-models orm

我有一个带有两个表的现有数据库,称为身份和帐户,我试图使用Django ORM进行管理。

身份与帐户有一对多的关系,我对这两个表建模如下:

class Identity(models.Model):
    class Meta:
        managed = False
        db_table = "Identities"
    i_id = models.AutoField(db_column = "I_ID", primary_key = True) 
    name = models.CharField(db_column = "DisplayName", max_length = 200)

class Account(models.Model):
    class Meta:
        managed = False
        db_table = "Accounts"
    name = models.CharField(db_column = "Name", max_length = 200, primary_key = True)
    identity = models.ForeignKey("Identity", db_column = "I_ID", blank = True, null = True, related_name = "accounts")

我的问题是,当我更新与帐户关联的身份时,与新身份相关联的所有帐户都会切换到新帐户:

old_identity = Identity.objects.create(name = "Old")
new_identity = Identity.objects.create(name = "New")
account_1 = Account.objects.create(name = "account_1", identity = old_identity)
account_2 = Account.objects.create(name = "account_2", identity = old_identity)

# change the identity for account_1:
account_1.identity = new_identity
account_1.save()

# read account_2 from DB and check identity
account_2 = Account.objects.get(name = "account_2")

# identity is now "New" also for account_2!
print account_2.identity.name 

如果我直接在数据库中执行相同的更新,则只有我更改的帐户更改了身份,而不是与身份相关联的所有帐户,因此这是Django引入的内容。

如果我更改identity字段时只更新了一个帐户,我该怎么办?

注意:为了清楚起见,数据库中的两个引用都已更改,这不是数据未刷新的问题或类似的问题。

1 个答案:

答案 0 :(得分:3)

检查已发布的相关代码中的问题。已发布的代码按预期运行:

创建表格:

(venv)dani@egg-v3:~/tmp/lldldl/paolo$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: mytest
  Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Creating table mytest_identity
    Creating table mytest_account
  Installing custom SQL...
  Installing indexes...
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying sessions.0001_initial... OK

运行:

(venv)dani@egg-v3:~/tmp/lldldl/paolo$ python manage.py shell
Python 2.7.3 (default, Feb 27 2014, 19:58:35) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from mytest.models import Account,Identity
>>> old_identity = Identity.objects.create(name = "Old")
>>> new_identity = Identity.objects.create(name = "New")
>>> account_1 = Account.objects.create(name = "account_1", 
                                       identity = old_identity)
>>> account_2 = Account.objects.create(name = "account_2", 
                                       identity = old_identity)
>>> 
>>> # change the identity for account_1:
>>> account_1.identity = new_identity
>>> account_1.save()
>>> 
>>> # read account_2 from DB and check identity
>>> account_2 = Account.objects.get(name = "account_2")
>>> 
>>> # identity is now "New" also for account_2!
>>> print account_2.identity.name 
Old
>>> 

使用django 1.7和sqlite3进行测试。也许某些数据库触发?发布/预保存信号?