Django .save() 恢复

发布于 2024-11-09 10:39:57 字数 1006 浏览 0 评论 0原文

我在模型中设置了一个称为状态的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埋情葬爱 2024-11-16 10:39:57

嗯...不应该是这样的:

originalCase = Case.objects.get(queue_num = swap)

是这样的:

originalCase = Case.objects.get(queue_num = 99)

如果您按原样复制并粘贴该代码,那就是您的问题。您实际上是在获取刚刚更改的内容并将其更改回来。

更新:

简化代码也可能有助于找出问题。从技术上讲,您不需要交换占位符。由于在最初获取所有对象时查询仅访问数据库一次,因此您可以执行如下操作:

cases = Case.objects.all()
for case in cases:
    if case.queue_num == original:
        case.queue_num = swap
    elif case.queue_num == swap:
        case.queue_num = original

    case.save()

或者如果您想保持相同的想法,您甚至可以执行以下操作(实际上可能更简单):

Case.objects.filter(queue_num=original).update(queue_num=99)
Case.objects.filter(queue_num=swap).update(queue_num=original)
Case.objects.filter(queue_num=99).update(queue_num=swap)

Hmmm... Shouldn't the following:

originalCase = Case.objects.get(queue_num = swap)

Be this:

originalCase = Case.objects.get(queue_num = 99)

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:

cases = Case.objects.all()
for case in cases:
    if case.queue_num == original:
        case.queue_num = swap
    elif case.queue_num == swap:
        case.queue_num = original

    case.save()

Or if you want to keep the same idea, you could even do the following (which might actually be simpler):

Case.objects.filter(queue_num=original).update(queue_num=99)
Case.objects.filter(queue_num=swap).update(queue_num=original)
Case.objects.filter(queue_num=99).update(queue_num=swap)
找回味觉 2024-11-16 10:39:57

从底部开始的第 2、3、4 行没有效果:

originalCase = Case.objects.get(queue_num = swap)
originalCase.queue_num = swap
originalCase.save()

line 2,3,4 from the bottom have no effect:

originalCase = Case.objects.get(queue_num = swap)
originalCase.queue_num = swap
originalCase.save()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文