基于计算值的Django计算 [英] Django calculation based on calculated value
问题描述
我再次需要您的帮助。我正在为我的作战小组进行一个项目。这是一个简单的排名网站。所以我们有球员,他们参加比赛并获得积分。现在我可以分配球员参加锦标赛,分配他们在比赛中所占的席位了。
I need your help once more. I am working on a project for my wargaming group. It is a simple ranking site. So we have players, they participate in tournaments and get points. I got to the point where I am able to assign players to tournaments, assign place they took at the tournament to their name.
现在我必须计算积分。算法很简单,但是我很难将值从锦标赛
模型传递到排名
。每个锦标赛都有一个计算得出的评分(基于其他因素,主要是更大的锦标赛,更大的评分),在其他模型中,我无法使用它,需要您的帮助。最重要的是,如果在锦标赛中更改评级值会强制更新所有相关计算,那将是非常棒的。
Now I have to calculate points. Algorithm is simple, but I have problems passing a value from Tournament
model to Ranking
. Each Tournament has a calculated rating (based on other things, mostly bigger tournament, bigger rating) and in other models, I was unable to use it and need your help with it. On top of that, it would be awesome if changing a rating value in Tournament would force an update of all dependent calculations.
所以我们有这样的模型:
So we have models like that:
class Player(models.Model):
class Meta:
name = models.CharField(max_length=50)
nicname = models.CharField(max_length=50,blank=True)
army = models.CharField(max_length=50)
def __unicode__(self):
return self.name
def __str__(self):
return self.name
class Tournament(models.Model):
class Meta:
name = models.CharField(max_length=100)
date = models.DateTimeField('date')
player_num = models.IntegerField
points = models.FloatField(default=1000.00)
def __unicode__(self):
return self.name
def __str__(self):
return self.name
然后我有这种排名模型:
And then I have a ranking model of this kind:
class TournamentStandings(models.Model):
tournament = models.ForeignKey(Tournament, on_delete=models.CASCADE)
player = models.ForeignKey(Player, on_delete=models.CASCADE)
player_place = models.FloatField
在admin.py中,我对TournamentAdmin进行计算:
In admin.py I do calculations for TournamentAdmin:
fields = ['name', 'date', 'player_num', 'points', 'get_rating']
def get_rating(self, obj):
return obj.points / 100.00
现在我想对TournamentStandingsAdmin进行计算:
And now I would like to make calculation for TournamentStandingsAdmin:
def player_points(self, obj):
return (obj.tournament.player_num/obj.player_place)* obj.tournament.get_rating
但是结果是错误
'Tournament'对象没有属性'get_rating'
'Tournament' object has no attribute 'get_rating'
所以我的猜测是我计算出的 get_rating 不是真实的模型字段,但是如何解决呢?
So my guess is my calculated get_rating is not a true model field but how to work around that?
推荐答案
如@Daniel所建议的,您需要在您的计算机上添加 get_rating
方法锦标赛
模型。
as @Daniel suggested you need to add get_rating
method to your Tournament
model.
class Tournament(models.Model):
name = models.CharField(max_length=100)
date = models.DateTimeField('date')
player_num = models.IntegerField
points = models.FloatField(default=1000.00)
....
def get_rating(self):
return obj.points / 100.00
之后,您可以使用 Tournament
对象调用该方法,如下所示:
After that you can call the method with a Tournament
object as follows:
obj.tournament.get_rating
这篇关于基于计算值的Django计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!