我目前正在学习 Django 并为此构建一个类似 twitter 的应用程序。我在 Profile 模型中使用了 ManyToManyField 来反映关注者:models.pyclass Profile(models.Model): """ Extension of User model to save additional information """ user = models.OneToOneField(User, on_delete=models.CASCADE) bio = models.TextField(max_length=500, blank=True) followers = models.ManyToManyField('self', related_name='Followers', blank=True, symmetrical=False) follower_count = models.IntegerField(default=0) following_count = models.IntegerField(default=0)现在我正在尝试检查用户是否已经在关注另一个用户(打开个人资料时,我可以在那里显示正确的关注/取消关注按钮)views.pydef profile(request, username): try: user = User.objects.get(username=username) user_profile = Profile.objects.get(user_id=user.id) except ObjectDoesNotExist: raise Http404("User does not exist") is_following = True if user.id in Profile.followers.all() else False return render(request, 'songapp/profile.html', {'user_profile': user_profile, 'user' : user, 'is_following': is_following})问题在于Profile.followers.all()当我得到以下 AttributeError 时:'ManyToManyDescriptor' object has no attribute 'all'我已经使用了搜索功能并阅读了长达 8 岁的结果,但我没有找到或理解相应的答案。非常感谢任何帮助
添加回答
举报
0/150
提交
取消