原子模型保存在django中 [英] atomic model save in django

查看:139
本文介绍了原子模型保存在django中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个模型问题,提交,UserResult。用户提交一个问题的答案,此答案由系统评分并存储在提交中。



一个问题的用户的得分在他的所有人中是最大的/她的提交。这个分数被保存在一个名为UserResult的模型中(它有三个字段,一个外键为问题,另一个为用户,一个分数为整数)



要做的是确保提交的内容保存相关的UserResult被更新。实际上最好不要在没有更新结果的情况下存储提交。因此,只有当UserResult正确更新时,才应保存提交。所以这个整个过程应该是原子的。

解决方案

在提交的中保存方法,调用 UserResult 更新方法。并将提交的保存方法标记为 @ transaction.atomic 。然后如果在更新UserResult时发生错误,则不会保存提交。


I've three models Question, Submission, UserResult. users submit an answer to a Question, this answer is scored by the system and stored in a Submission.

The score of a user for one question is maximum amongst all of his/her submissions. This score is saved in a model called UserResult(it has three fields, a foreign key to Question, another to User and a score which is an integer)

What I want to do is to make sure when a submission is saved the related UserResult is updated. Actually it's better not to store a submission when the result is not updated. So the submission should be saved only if UserResult is updated correctly. So this whole process should be atomic. What is the proper way of handling this?

解决方案

In the Submission's save method, call UserResult update method. And mark Submission's save method as @transaction.atomic. Then if an error happen while updating UserResult, Submission won't be saved.

这篇关于原子模型保存在django中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆