在Django 2.0中过滤外键

时间:2018-04-28 11:19:13

标签: python django postgresql foreign-keys key

所以我需要过滤用户发布的帖子,用户当前登录的用户是谁。

这是我的模特:

class ProfileDetails(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    provider = models.CharField(max_length=20, null=True, blank=True)
    firstname = models.CharField(max_length=25, null=True, blank=True)
    lastname = models.CharField(max_length=25, null=True, blank=True)
    username = models.CharField(max_length=24, null=True, blank=True, unique=True)

    def __str__(self):
        return str(self.user)


class Posts(models.Model):
    post_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    user = models.ForeignKey(ProfileDetails, on_delete=models.CASCADE, null=True)
    text = models.TextField(max_length=280, null=True, blank=True)
    video = models.CharField(max_length=24, null=True, blank=True)
    timestamp = models.DateTimeField(default=datetime.datetime.now, blank=True)

    def __str__(self):
        return str(self.user)


class Connection(models.Model):
    follower = models.ForeignKey(ProfileDetails, related_name='follower', on_delete=models.SET_NULL, null=True)
    following = models.ForeignKey(ProfileDetails, related_name='following', on_delete=models.SET_NULL, null=True)
    # follower = models.ManyToManyField(ProfileDetails, related_name='follower')
    # following = models.ManyToManyField(ProfileDetails, related_name='following')
    date_created = models.DateTimeField(auto_now_add=True, null=True)

    def __str__(self):
        return str(self.follower)

然后在我的观点中,我的Python / Django知识有点指导我写这样的东西(特别注意最后一点,我尝试过滤它,所以它只从用户那里得到细节我跟着。

def index(request):
    checkuser = request.user
    print(checkuser)
if ProfileDetails.objects.filter(user=checkuser):
            print("user previously logged in")
            sns = get_object_or_404(SocialAccount, user=checkuser)
            autoupdateprofile = get_object_or_404(ProfileDetails, user=checkuser)
            autoupdateprofile.lastlogin = datetime.datetime.now()
            autoupdateprofile.save(update_fields=["lastlogin"])

            # print(sns.provider)

            details = get_object_or_404(ProfileDetails, user=checkuser)
            videos = Posts.objects.filter(media=True, imade=True).order_by("-timestamp")[0:6]
            followercount = Connection.objects.filter(follower=details).count()
            follows = Connection.objects.filter(follower=details)

            followerposts = Posts.objects.filter(user=follows)



            for a in followerposts:

                print(a)

return render (request, 'index.html', context)

但是,这似乎不起作用。我试图让这个可爱的错误试图让我使用模型ProfileDetails中的数据 - 这显然没有任何意义,因此我可以指示我在关联中跟随的用户。

Exception Value:    
Cannot use QuerySet for "Connection": Use a QuerySet for "ProfileDetails".

几天来一直在努力,不再确定要搜索什么。

如果它有所不同,我在Postgresql上使用Python 3.6和Django 2.0.4。

建议将不胜感激。 :)

提前致谢。 罗纳德

1 个答案:

答案 0 :(得分:1)

此代码进入并获取当前用户所关注的用户的所有帖子(即,当前用户为follower的人):

Posts.objects.filter(user__following__follower=ProfileDetails.objects.get(user=self.request.user))