Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to set query for show followed posts in home page

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading