开发者

How would I make this query in Django?

开发者 https://www.devze.com 2023-02-05 00:14 出处:网络
class Content(models.Model): author = models.ForeignKey(\'auth.User\') stamp = models.CharField(max_length=50)开发者_C百科
class Content(models.Model):
    author = models.ForeignKey('auth.User')
    stamp = models.CharField(max_length=50)开发者_C百科

class Comments(models.Model):
    content = models.ForeignKey(Content)
    message = models.TextField()

I want to get all the comments for content that the current logged in user created. But this doesn't work:

Comments.objects.filter(content.author = request.user)


Use field lookups:

Comments.objects.filter(content__author=request.user)

Django offers a powerful and intuitive way to "follow" relationships in lookups, taking care of the SQL JOINs for you automatically, behind the scenes. To span a relationship, just use the field name of related fields across models, separated by double underscores, until you get to the field you want.

0

精彩评论

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