django - 为什么 Django 模型字段在遍历关系时会不同步?

标签 django orm django-models

我在访问已更改的模型字段、遍历 Django 1.3 应用程序中的关系时遇到问题。提交到数据库的更改不会被内存中的对象反射(reflect)。我使用 Auth Middleware 中的 User() 对象,将其链接到自定义 Profile() 对象:

User() <---one-to-one---> Profile()

访问 User() 的 email 字段时出现问题:

$ python manage.py shell
>>> from django.contrib.auth.models import User
>>> from myproject.myapp.models import Profile
>>>
>>> user = User.objects.get(pk=1)
>>> profile = user.profile
>>> user.email
u'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6f00030b2f0a170e021f030a410c0002" rel="noreferrer noopener nofollow">[email protected]</a>'                  # OK.
>>> profile.user.email
u'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="741b181034110c15190418115a171b19" rel="noreferrer noopener nofollow">[email protected]</a>'                  # OK.
>>> 
>>> user.email = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8c6cddfe8cdd0c9c5d8c4cd86cbc7c5" rel="noreferrer noopener nofollow">[email protected]</a>'  # 1) Changes email address.
>>> user.save()                     # 2) Commits to database.
>>> user.email
'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6c02091b2c09140d011c0009420f0301" rel="noreferrer noopener nofollow">[email protected]</a>'                   # 3) Changes reflected in user.email. Good.
>>> profile.user.email
u'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="036c6f6743667b626e736f662d606c6e" rel="noreferrer noopener nofollow">[email protected]</a>'                  # 4) Wrong. This is the old incorrect email.
>>> user.profile.user.email
u'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2cdcec6e2c7dac3cfd2cec78cc1cdcf" rel="noreferrer noopener nofollow">[email protected]</a>'                  # 5) Also wrong.
>>> 
>>> profile = Profile.objects.get(user=user)
>>> profile.user.email
u'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2f414a586f4a574e425f434a014c4042" rel="noreferrer noopener nofollow">[email protected]</a>'                  # 6) If we re-query the DB, things are OK.

为什么第 4 步和第 5 步中的内容不同步?我对 #5 感到困惑,从保存的用户对象到配置文件,然后返回同一个保存的用户对象。

显然存在某种缓存,但我不确定其背后的逻辑/算法。任何人都知道发生了什么,以及在代码中解决这个问题的最佳方法?感谢您提供的任何见解! :)

最佳答案

SingleRelatedObjectDescriptor 负责透明地查找相关对象的描述符检查 _<fieldname>_cache 中相关对象的缓存版本在 Model 实例中,在第一次检索时对其进行缓存。

Django 的 ORM 不使用身份映射,因此对一个模型实例的更改不会自动反射(reflect)在现有引用所保留的其他实例中。

关于django - 为什么 Django 模型字段在遍历关系时会不同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5784316/

相关文章:

django - 错误 'NoneType' 对象没有属性 '__dict__'

python - Django:动态 LOGIN_URL 变量

django - Django Admin 中的表单为模型上的 "notes"或 "comments"

Python Django 从 DateField 按月获取不同的查询集

c# - 任何 ORM 都可以在运行时以编程方式更改其连接字符串吗?

python - SQLAlchemy:单表继承,子项中的同一列

orm - 带有 SQL 后端的 Lisp OODB - 或者好的 ORM

python - django instance.id=None 上传图片时

django-allauth:如何为我的域添加站点?

python - 如何禁止将软件包添加到 Pipenv.lock 并由 Pipenv 安装?