Reputation: 1270
I am building a BlogApp and I am stuck on an Error.
What i am trying to do :-
I am trying to exclude
the users that are my followers
from the BlogPost Page
.
I am using exclude
method to exclude the users.
When i go to the browser then it keep showing :-
Related Field got invalid lookup: user
models.py
class Post(models.Model):
post_owner = models.ForeignKey(User,default='',null=True,on_delete = models.CASCADE)
post_title = models.CharField(max_length=500,default='' )
views.py
def posts(request,user_id):
followed_users= request.user.profile.followers.all()
posts = Post.objects.filter(date_added__lte=now).exclude(post_owner__user=followed_users)
context = {'posts':posts}
return render(request, 'blogpost.html', context)
I don't know what i am doing wrong.
Any help would be Appreciated.
Upvotes: 0
Views: 1387
Reputation: 4432
No need to use post_owner__user
lookup since post_owner
is already an aliace to User
model. Also you can't filter by equality here becase on the right side of the expression you have not a single instance of User
, but a queryset.
Try this instead:
followed_users= request.user.profile.followers.all()
followed_users = list(followed_users.values_list('id', flat=True))
posts = Post.objects.filter(date_added__lte=now).exclude(post_owner_id__in=followed_users)
First line will return only User
ID field values that you can use in exclude with in expression.
Upvotes: 1