Django .save() 恢复
我在模型中设置了一个称为状态的 InteferField。我有一个方法是两次切换两行状态的值。
Row one -> status = 1
Row two -> status = 2
现在,我想,如果我将第一行的状态切换到某个无法达到的范围(99),我可以使用它作为中间人来切换第二行,然后切换第一行。
Get status = 1 -> Row one
Get status = 2 -> Row two
Set Row one -> status = 99
Save Row one
Set Row two -> status = 1
Save Row two
Get status = 99 -> Row one
Set Row one -> status = 2
Save Row one
奇怪的是,数据又恢复了。如果我只是将第一行的状态更改为 99,它将更改为 99,然后稍后又恢复为原始值。我们不确定为什么会发生这种情况,但事实证明这根本没有任何结果。
original = 1
swap = 2
originalCase = Case.objects.get(queue_num = original)
#swapCase = Case.objects.get(queue_num = swap)
originalCase.queue_num = 99
originalCase.save()
#swapCase.queue_num = original
#swapCase.save()
#originalCase = Case.objects.get(queue_num = 99)
#originalCase.queue_num = swap
#originalCase.save()
return HttpResponse(Case.objects.filter(queue_num__gt=0).order_by('queue_num'))
是否是因为我们查询速度太快而没有及时更新以进行下一次更新?还是我的逻辑有问题?
I have an InteferField set in models called the status. I have a method that is two switch the value of two rows statuses.
Row one -> status = 1
Row two -> status = 2
Now, I figure, if I switch row one's status to some unattainable reach (99) I could use that as the middle man to switch row two and then switch row one.
Get status = 1 -> Row one
Get status = 2 -> Row two
Set Row one -> status = 99
Save Row one
Set Row two -> status = 1
Save Row two
Get status = 99 -> Row one
Set Row one -> status = 2
Save Row one
The strange thing is, the data reverts. If I just change the status of Row one to 99, it will change to 99 and then a moment later, back to its original value. We're not sure why this is happening, but it turns out nothing is come from this at all.
original = 1
swap = 2
originalCase = Case.objects.get(queue_num = original)
#swapCase = Case.objects.get(queue_num = swap)
originalCase.queue_num = 99
originalCase.save()
#swapCase.queue_num = original
#swapCase.save()
#originalCase = Case.objects.get(queue_num = 99)
#originalCase.queue_num = swap
#originalCase.save()
return HttpResponse(Case.objects.filter(queue_num__gt=0).order_by('queue_num'))
Is it because we're querying to fast and it's not updating in time for the next update? Or is there a flaw in my logic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯...不应该是这样的:
是这样的:
如果您按原样复制并粘贴该代码,那就是您的问题。您实际上是在获取刚刚更改的内容并将其更改回来。
更新:
简化代码也可能有助于找出问题。从技术上讲,您不需要交换占位符。由于在最初获取所有对象时查询仅访问数据库一次,因此您可以执行如下操作:
或者如果您想保持相同的想法,您甚至可以执行以下操作(实际上可能更简单):
Hmmm... Shouldn't the following:
Be this:
If you copy and pasted that code as is, that's your problem. You're essentially fetching the one you just changed and changing it back.
UPDATE:
Simplifying your code may help ferret out the problem, as well. You don't technically need a swap placeholder. Since the query hits the database only once when initially fetching all the objects, you could do something like the following:
Or if you want to keep the same idea, you could even do the following (which might actually be simpler):
从底部开始的第 2、3、4 行没有效果:
line 2,3,4 from the bottom have no effect: