0

There is 3 models, Article(post) Follow(To follow a user) User, How can I get all the post of user I'm following?

class Article(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    slug = models.SlugField(db_index=True, unique=True, max_length=255)
    title = models.CharField(max_length=255)
    subtitle = models.CharField(blank=True, max_length=400)
    body = RichTextUploadingField()


class Follow(models.Model):
    user_from = models.ForeignKey(User,
                                  related_name='rel_from_set',
                                  on_delete=models.CASCADE)

    user_to = models.ForeignKey(User,
                                related_name='rel_to_set',
                                on_delete=models.CASCADE)

    created = models.DateTimeField(auto_now_add=True,
                                   db_index=True)


class User(models.Model):
    pass
1
  • You need the Post from all the users you are following right? Commented May 4, 2020 at 8:37

1 Answer 1

1

Try this to get all the Post from all the users followed by the request.user:

Article.objects.filter(author_id__in=self.request.user.rel_from_set.all().values_list('user_to_id'), flat=True)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.