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):
...
精彩评论