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.