简体   繁体   English

Django-如何检索已登录用户好友的帖子数据

[英]Django-How to retrieve posts data of logged in user friends

I am working on a Social network project!! 我正在做一个社交网络项目!! It has models that saves User data ,Friends data and Posts data 它具有保存用户数据,朋友数据和帖子数据的模型

Users -> User Info
Friends -> User friends
Media -> All Posts from users

How do i get the posts of friends of logged-in user . 如何获得已登录用户的朋友的帖子。

class Friends(models.Model):
    """Model for saving relationship of user and friends"""
    request_id = models.ForeignKey(User,on_delete=models.CASCADE,related_name='current_user')
    friend_id = models.ForeignKey(User,on_delete=models.CASCADE,related_name='user_friend')
    created_on = models.DateTimeField(auto_now_add=True,auto_now=False)

class MediaDB(models.Model):
    """All User Feed Items"""
    username = models.ForeignKey(User,on_delete=models.CASCADE,)
    m_url = models.FileField(blank=True,null=True)
    timeStamp = models.DateTimeField(auto_now_add=True,auto_now=False)
    location = models.CharField(max_length=30)
    likes_count = models.BigIntegerField(default=0)

You can get all the posts for the friends of the logged-in user by following the relationships: 您可以通过以下关系获取登录用户朋友的所有帖子:

friends_posts = MediaDB.objects.filter(username__user_friend__request_id=request.user)

Note that some of your naming is a bit odd; 请注意,您的某些命名有些奇怪; the username field on MediaDB points to the user, so it should be called just user , and the fields on Friends are even stranger; username上MediaDB场指向用户,所以它应该被称为只是user ,并在朋友的领域甚至是陌生人; there is no "request" involved, and they are not "ids", and the reverse relations don't at all explain what they are. 没有“请求”,它们也不是“ id”,反向关系根本无法解释它们是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM