0

I'm trying to get post objects from all the groups a user follows. I was able to get a list of groups the user follows and a queryset of all the posts but I'm having difficulty passing the objects to a template.

My models:

class Post(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.PROTECT)
    title = models.CharField(max_length = 120, default = '', blank = False)
        body = models.TextField()

class Community(models.Model):
    title = models.CharField(max_length = 50, default = '', unique = True)
    followers = models.ManyToManyField(Account, related_name = "community_followers")
    posts = models.ManyToManyField(Post, blank = True, related_name = "community_posts")

    def __str__(self):
        return self.title

class Account(AbstractBaseUser):
    username = models.CharField(max_length = 30, unique = True)
    email = models.EmailField(verbose_name = "email", max_length = 50, unique = True)
    date_joined = models.DateTimeField(verbose_name = "date joined", auto_now_add = True)
    last_login = models.DateTimeField(verbose_name = "last login", auto_now = True) 
    is_admin = models.BooleanField(default = False)
    is_active = models.BooleanField(default = True)
    is_staff = models.BooleanField(default = False)
    is_superuser = models.BooleanField(default = False)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']


    objects = MyAccountManager()

The view:

user = request.user
user_communities = user.community_followers.all()
queries = []
for community in user_communities:
    queries.append(community.posts.all())

context['posts'] = queries

return render(request, "home/index.html", context)

Template:

{% for post in posts %}
    {{ post.title }}
    <hr>
{% endfor %}

None of the posts get rendered in the template and I cant seem to order them either. I figured there must be something wrong in my implementation.

T.I.
  • 1
  • 2
  • if you've already tested that `queries` contains the results, could you add the template example here ? – PRMoureu Jul 19 '19 at 16:00
  • I added the template block in the question. When I print out the queryset I get: ]> – T.I. Jul 19 '19 at 17:26

1 Answers1

0

Assuming Account is the AUTH_USER_MODEL, you can do this in one query:

Post.objects.filter(community_posts__followers=request.user)

(If not, please post the Account model.)

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Didn't work how? What do you get? Can you print the result of this query? – Daniel Roseman Jul 19 '19 at 17:26
  • The queryset seems to be empty. When I print it out I get nothing – T.I. Jul 19 '19 at 17:30
  • Can you look at your database and see how many records you expect to be in the queryset? It might help to get the sql of the query: https://stackoverflow.com/questions/3748295/getting-the-sql-from-a-django-queryset The query Daniel posted looks correct. – Tim Saylor Jul 19 '19 at 18:41
  • I changed Account in Community to settings.AUTH_USER_MODEL and for some reason it now works. Thanks – T.I. Jul 19 '19 at 18:51