python - 无法更新 Django 1.9 BooleanField

标签 python django

我有以下模型

class UserProfile(models.Model):

    user = models.OneToOneField(User, related_name='profile')
    force_password_change = models.BooleanField(default=False)

出于某种原因,我根本无法更改 BooleanField。我想在没有模型形式的 View 中执行此操作。这是我从 Django 命令行获得的输出

>>> new_user = User.objects.get(username="bob")
>>> new_user
<User: bob>
>>> type(new_user)
<class 'django.contrib.auth.models.User'>
>>> new_user.profile
<UserProfile: bob>
>>> type(new_user.profile)
<class 'accounts.models.UserProfile'>
>>> new_user.profile.force_password_change
False
>>> new_user.profile.force_password_change = True
>>> new_user.save()
>>> new_user.profile.force_password_change
False
>>>

那么我在这里错过了什么?我确信这是愚蠢的事情,但任何帮助都会很棒。

更新:

如果更新个人资料字段,也会出现同样的问题。

>>> new_user.profile.force_password_change
False
>>> new_user.profile.force_password_change = True
>>> new_user.profile.save()
>>> new_user.profile.force_password_change
False
>>>

更新2:

也尝试过这个:

>>>
>>> new_user.profile.force_password_change = True
>>> new_user.profile.save()
>>> new_user = User.objects.get(pk=new_user.pk)
>>> new_user.profile.force_password_change
False
>>>

我想我可以提供的最后一点有用信息是我可以通过管理面板完全更改force_password_change字段。当我尝试 user.profile.save() 时,好像保存操作没有发生?但我没有收到任何我注意到的错误。

最佳答案

您是否尝试过直接保存 UserProfile 对象?

p = UserProfile.objects.get(user=new_user)
p.force_change_password = True
p.save()

print new_user.profile.force_change_password

请原谅我的简短...我正在手机上输入此内容。

关于python - 无法更新 Django 1.9 BooleanField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37353818/

相关文章:

python - 为什么 Pycharm 可以找到我的模块,但不能运行它?

python - 导入 Dash : "ImportError: DLL load failed while importing _brotli: The specified module could not be found." 时出错

python - 如果先前的查询因超时而失败,我将无法使用 aiohttp session

Python Django 1.9迁移错误 'error creating new content types...'

python - Django - 如何基于具有 OneToOneField 的用户模型查询用户个人资料

python - admin.autodiscover 实际上做了什么?

python - 使用 cmp 或 == 比较列表

python - uWSGI 运行错误的 Python 版本

Django如何写PositiveDecimalField

mysql - 插件 caching_sha2_password 无法加载 :/mariadb19/plugin/caching_sha2_password. 所以:无法打开共享对象文件