开发者

Django passing arguments to custom method in models (or accessing request)

I'm trying to implement some customize login through the custom methods in Django's models. I want to know if its possible to:

  • Get request.user in a custom method
  • Get the user that made the request in the method
  • Or pass an argument to the custom me开发者_C百科thod

Thinking in doing something like this:

class OneModel(models.Model):
    (...)

    def viewed(self):
        profile = request.user.profile
        viewed = self.viewed_episodes.filter(user=profile).exists()
        if viewed: return True
        else: return None

Another possibility that came to my mind is this:

class OneModel(models.Model):
    (...)

    def viewed(self, user):
        profile = user.profile
        viewed = self.viewed_episodes.filter(user=profile).exists()
        if viewed: return True
        else: return None

But I think neither of this are possible. Maybe what I need is a template tag?


Second one is correct.

def viewed(self, user):
    return self.viewed_episodes.filter(user=user.profile).exists() or None
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜