我有一个带有 created_at 字段的客户端模型。我想从 shell 更新客户端的 created_at。我做了以下事情:
>>> c = Client.objects.all()
>>> c[1].created_at
>>> # I get nothing here since it's not set yet
>>> from django.utils import timezone
>>> c[1].created_at = timezone.now()
>>> c[1].save()
>>> c[1].created_at
>>> # I still get nothing here. Even when I reload the queryset, I get nothing.
Run Code Online (Sandbox Code Playgroud)
但是,当我用 做同样的事情时c = Client.object.get(id=1),它就起作用了。为什么会发生这种情况?
您需要将查询集对象分配给它自己的实例然后它会保存,您在上面所做的只是编辑查询集项目
c = Client.objects.all()
obj = c[1]
obj.created_at = timezone.now()
obj.save()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3009 次 |
| 最近记录: |