ValueError:不能将Queryset用于“";将Queryset用于“". [英] ValueError: Cannot use Queryset for "": Use a Queryset for ""
问题描述
我正在使用这些模型在一个小项目上工作,我试图找出一种方法来获取与当前经过身份验证的用户关注的用户相关的所有帖子集.
I'm working on a little project using these models here and I'm trying to figure out a way to get a set of all the posts associated with users the currently authenticated user is following.
但我不断得到:
不能将QuerySet用于个人资料":将QuerySet用于用户".
Cannot use QuerySet for "profile": Use a QuerySet for "User".
class Profile(models.Model):
user = models.OneToOneField(User)
isInstructor = models.BooleanField(default=False)
isTutor = models.BooleanField(default=False)
isStudent = models.BooleanField(default=False)
isAdmin = models.BooleanField(default=False)
following = models.ManyToManyField('self', related_name = "followers", blank=True, symmetrical=False)
profile_image = ImageField(upload_to=get_image_path, blank=True, null=True)
class Post(models.Model):
title = models.CharField(max_length=100)
topic = models.CharField(max_length=50)
description = models.CharField(max_length=1200)
poster = models.ForeignKey(User, related_name="posts")
likes = models.IntegerField(default=0)
created = models.DateTimeField(auto_now_add=True)
tags = models.ManyToManyField(Tag, blank=True, related_name="posts")
def __str__(self):
return self.title
这就是不断给我错误的原因.
This is what keeps giving me the error.
current_user = Profile.objects.get(user = self.request.user)
Post.objects.filter(poster__in = current_user.following.all())
我搜索了一个发现,发现每当您要按事物列表进行过滤时,都必须使用__in运算符.但是我一直收到同样的错误.我们将不胜感激,以帮助您解释错误的含义以及如何解决该错误.
I searched around an found out that I had to use the __in operator whenever you want to filter by a list of things. But I keep getting the same error. Any help with explaining what the error means and what I can do to get around it would be much appreciated.
推荐答案
也许可以尝试这样的事情,
Maybe try something like this,
Post.objects.filter(poster__id__in=current_user.following.all().values_list('id'))
这篇关于ValueError:不能将Queryset用于“";将Queryset用于“".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!