a hard question about django ORM

23 views
Skip to first unread message

时空路由器

unread,
Aug 6, 2017, 6:06:56 AM8/6/17
to Django users
there are three classes

class User(models.Model):
  name
= models.CharField()

class Feed(models.Model):
  user
= models.ForeignKey(User)

class Comment(models.Model):
  user
= models.ForeignKey(User)
  feed
= models.ForeignKey(Feed)


How to get all my feeds and all the coments of them, but the user of comment are not mine

lemme smash

unread,
Aug 6, 2017, 10:06:08 AM8/6/17
to Django users
what the problem here?
you can get all your Feeds by
feeds = Feed.objects.filter(user=me)
 and then all the comments for each feed
comments = feed.comment_set.exclude(user=me)
or what?
if you want to get only comments, you can do
comments = Comment.objects.filter(feed__in=feeds).exclude(user=me)
Reply all
Reply to author
Forward
0 new messages