我正在研究我的第一个django项目,这是一项体育博彩游戏。
我的模特是:
import UIKit
class AccountViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.view.backgroundColor = .clear
func tableView(_ tableView: UITableViewCell, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 1 && indexPath.row == 0 {
print("pressed")
}
}
}
}
计算分数的逻辑是:
class Game(models.Model):
home_team = models.CharField(max_length=200)
away_team = models.CharField(max_length=200)
home_goals = models.IntegerField(default=None)
away_goals = models.IntegerField(default=None)
class Bet(models.Model):
gameid = models.ForeignKey(Game, on_delete=models.CASCADE)
userid = models.ForeignKey(User, on_delete=models.CASCADE)
home_goals = models.IntegerField()
away_goals = models.IntegerField()
score = models.IntegerField(default=None, null=True)
我能够使用数据库视图轻松解决它,该数据库视图将所有数据组合在一个表中,但似乎它与django迁移无法很好地合作。
所以我在更新游戏目标后考虑了sql触发器,但后来我不知道如何传递用户id的条件。
第二个想法是创建额外的'得分'表:
WHEN polls_bet.home_goals = polls_game.home_goals AND polls_bet.away_goals = polls_game.away_goals THEN 2
WHEN polls_game.home_goals > polls_game.away_goals AND polls_game.home_goals > polls_game.away_goals THEN 1
WHEN polls_bet.home_goals < polls_bet.away_goals AND polls_game.home_goals < polls_game.away_goals THEN 1
ELSE 0
但是我再一次不知道如何计算分数。 请告知如何正确完成此操作,而不使用sql视图。我很感激所有答案!
答案 0 :(得分:1)
您可以在Bet模型上定义'score'属性以轻松解决此问题。请参阅文档here。
您的属性实现类似于:
@property
def score(self):
if (self.home_goals == self.game__home_goals and
self.away_goals == self.game__away_goals):
return 2
if (self.game__home_goals > self.game__away_goals):
return 1
if (self.home_goals < self.away_goals and
self.game__home_goals < self.home_goals):
return 1
return 0
另外,外键关系的正常命名对象是小写的模型名称。所以它变成'游戏'和'用户'而不是'gameid'和'userid'。另外我相信你在第二个条件下有一些错别字。