Django 根据之前的查询选择数据库对象
我目前正在开发一个项目,其中有推特风格的单向关注,并且我无法弄清楚如何根据用户关注的人在数据库中选择“帖子”。
我正在使用默认的用户功能,然后这是我的以下模型:
class Following(models.Model):
user = models.ForeignKey(User, related_name="user")
following = models.ForeignKey(User, related_name="following")
和我的帖子模型:
class Post(models.Model):
title = models.CharField(max_length=100)
text = models.TextField()
user = models.ForeignKey(User)
date_time = models.DateTimeField(auto_now=True)
最后,我认为我正在尝试开始工作的片段:
def home(request):
following = Following.objects.filter(user=request.user)
posts = Post.objects.filter(/*not sure what to put here*/)
我整夜都在努力让这个工作我真的看不到解决方案,所以任何帮助都会很棒。
如果您可能需要查看我未在此处发布的任何内容,这个项目的github存储库在这里
I'm currently working on a project where there is twitter-style one way following, and I've been unable to figure out how to select 'posts' in the database based on who the user is following.
I'm using the default user functionality, and then this is my following model:
class Following(models.Model):
user = models.ForeignKey(User, related_name="user")
following = models.ForeignKey(User, related_name="following")
and my posts model:
class Post(models.Model):
title = models.CharField(max_length=100)
text = models.TextField()
user = models.ForeignKey(User)
date_time = models.DateTimeField(auto_now=True)
Finally, the snippet from my view that I'm trying to get to work:
def home(request):
following = Following.objects.filter(user=request.user)
posts = Post.objects.filter(/*not sure what to put here*/)
I've been trying all night to get this working and I can't really see a solution, so any help would be awesome.
if there's anything that you might need to look at that I haven't posted here, the github repository for this project is here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的 django 已经生锈了,但这里是:
My django is getting rusty, but here goes: