I want to make query to show all followed posts in the main page, could you help me in doing this?
Here's my file models.py:
class Relation(models.Model):
from_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='follower')
to_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='following')
created = models.DateTimeField(auto_now_add=True)
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
avatar = models.FileField(default='default.jpg', verbose_name='avatar')
age = models.PositiveSmallIntegerField(default=0)
location = models.CharField(max_length=30, blank=True)
work_at = models.TextField(null=True, blank=True)
bio = models.TextField(null=True, blank=True)
Thanks!
CodePudding user response:
checkout this code:
followed_people = Relation.objects.filter(from_user=request.user).values('to_user')
posts = Post.objects.filter(
user__in=followed_people
) | Post.objects.filter(user=request.user)