如何显示我关注的所有用户?
How can I show all users I follow?
我有模特:
个人资料模式
class Profile(AbstractUser):
following = models.ManyToManyField("self", through=UserFollowing, related_name="followers",
verbose_name=_("following"), symmetrical=False)
和用户关注模型
class UserFollowing(models.Model):
following_from = models.ForeignKey("Profile", related_name="following_from", on_delete=models.CASCADE,
verbose_name=_("Following from"))
follow_to = models.ForeignKey("Profile", related_name="follow_to", on_delete=models.CASCADE,
verbose_name=_("Following to"))
created = models.DateTimeField(auto_now_add=True, db_index=True)
如何显示我关注的所有个人资料?
我如何设置查询集以显示所有关注者和关注的个人资料?
Profile.objects.filter... ?
您将希望使用当前会话的已验证用户进行过滤
Profile.objects.filter(following=User.id)
要获取您关注的所有个人资料,请尝试以下操作:
Profile.objects.get(pk=request.user).following.all()
或者:
您关注的所有个人资料:
Profile.objects.get(pk=request.user).following_from.all()
所有个人资料都关注您:
Profile.objects.get(pk=request.user).follow_to.all()
我有模特:
个人资料模式
class Profile(AbstractUser):
following = models.ManyToManyField("self", through=UserFollowing, related_name="followers",
verbose_name=_("following"), symmetrical=False)
和用户关注模型
class UserFollowing(models.Model):
following_from = models.ForeignKey("Profile", related_name="following_from", on_delete=models.CASCADE,
verbose_name=_("Following from"))
follow_to = models.ForeignKey("Profile", related_name="follow_to", on_delete=models.CASCADE,
verbose_name=_("Following to"))
created = models.DateTimeField(auto_now_add=True, db_index=True)
如何显示我关注的所有个人资料? 我如何设置查询集以显示所有关注者和关注的个人资料?
Profile.objects.filter... ?
您将希望使用当前会话的已验证用户进行过滤
Profile.objects.filter(following=User.id)
要获取您关注的所有个人资料,请尝试以下操作:
Profile.objects.get(pk=request.user).following.all()
或者:
您关注的所有个人资料:
Profile.objects.get(pk=request.user).following_from.all()
所有个人资料都关注您:
Profile.objects.get(pk=request.user).follow_to.all()