开发者

Django - Model Helper Property for current user

开发者 https://www.devze.com 2023-04-11 04:00 出处:网络
I\'m looking for a way to get an object filtered with the current logged in user from a 开发者_C百科model helper property

I'm looking for a way to get an object filtered with the current logged in user from a 开发者_C百科model helper property

Let me give an example

class Exo(models.Model):
  """Exercises"""
  text = models.TextField()

class Score(models.Model):
  """Scores of users by exercise"""
  exo = models.ForeignKey(Exo)
  user = models.ForeignKey(User)
  score = models.IntegerField()
  class Meta:
    unique_together = (('exo', 'user',),)

From the model Exo I'd like to be able to get the score of the logged in user

class Exo(models.Model):
  """Exercises"""
  text = models.TextField()
  def get_logged_score():
    return self.score_set.filter(user=current_logged_user)[0]

From the Exo object, how can I get the score of the current_logged_user ?

Edit: The idea is to use get_logged_score from a template, so I can't pass any parameter


By passing it in.

def get_logged_score(self, user):
   ...
0

精彩评论

暂无评论...
验证码 换一张
取 消