将计算值保存为模型字段

时间:2017-12-07 22:35:06

标签: django django-models

作为我的预告片的跟进question我有一个新的。如何将此计算值保存为模型字段。我想在我的视图和模板中使用它来按此字段排序列表。

我的模特:

class Tournament(models.Model):
   name = models.CharField(max_length=100)
   date = models.DateTimeField('date')
   player_num = models.IntegerField(verbose_name="")
   points = models.FloatField(default=1000.00)
   def get_rating(self):
       return self.points / 1000.00

class TournamentStandings(models.Model):
   tournament = models.ForeignKey(Tournament, on_delete=models.CASCADE)
   player = models.ForeignKey(Player, on_delete=models.CASCADE)
   player_place = models.FloatField(verbose_name=u"")
   player_points = models.FloatField(verbose_name="", 
   blank=True) #added for testing to save the calculated value in it
   @property 
   def get_player_points(self, obj):
       return obj.tournament.player_num * obj.tournament.get_rating() - 
       obj.tournament.get_rating()*(obj.player_place - 1.00)

   def save(self, *args, **kwargs):
       self.player_points = self.get_player_points   
       super(TournamentStandings, self).save(*args, **kwargs)



   def __float__(self):
     return self.player_points

有趣的是,在管理员列表中,我有一个列正确计算player_points但是当我添加新模型实例并尝试保存它时,我收到此错误:'TournamentStandings' object has no attribute 'get_player_points'。是不是因为我想做一个" self"保存,我的计算是(self,obj)??任何提示都很好。

2 个答案:

答案 0 :(得分:0)

get_player_points()是一种方法,需要使用括号。

    val photoURI: Uri = FileProvider.getUriForFile(this, "com.packagename.provider",
            File(this.cacheDir.path + "/Report.pdf"))

    val emailIntent = Intent(Intent.ACTION_SENDTO)
    emailIntent.data = Uri.parse("mailto:")
    emailIntent.putExtra(Intent.EXTRA_STREAM, photoURI)
    emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf("some@domain.com"))
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject")
    emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    emailIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
    startActivity(emailIntent)

答案 1 :(得分:0)

为我的问题发布有效的解决方案。不需要括号。

首先我修复了锦标赛模型,因此我可以将get_rating保存为字段:

class Tournament(models.Model):
name = models.CharField(max_length=100)
rating = models.FloatField(verbose_name="Rating", blank=True)

@property
def get_rating(self):
    return (self.points) / (1000.00)
def save(self, *args, **kwargs):
    self.rating = self.get_rating 
    super(Tournament, self).save(*args, **kwargs)
def __float__(self):
    return self.rating

当我有这个时,我试图将它复制到第二个模型。问题是由于我在计算中调用的相关obj,我无法使其工作。但!我已设法将此值分配给get_player_points内的变量,现在所有变量都按预期工作:

class TournamentStandings(models.Model):
    tournament = models.ForeignKey(Tournament, on_delete=models.CASCADE)
    player = models.ForeignKey(Player, on_delete=models.CASCADE)
    player_place = models.FloatField(verbose_name="")
    player_points = models.FloatField(verbose_name="", blank=True)

@property
def get_player_points(self):
    player_num = float(self.tournament.player_num)
    rating = float(self.tournament.rating)
    player_points = float((rating)*(player_num)- (rating)*(self.player_place -1.00))
return player_points
    def save(self, *args, **kwargs):
    self.player_points = self.get_player_points
    super(TournamentStandings, self).save(*args, **kwargs)

def __float__(self):
    return self.player_points

这有效!我对改进的任何想法都很好。