[Answered ]-How to get model-objects using other models in django

1👍

You can filter with:

from django.contrib.auth.decorators import login_required

@login_required
def following_page(request):
    posts = Post.objects.filter(poster__following__follower=request.user)
    return render(request, 'network/index.html', {
        'posts': posts
    })

Since you use the logged in user, one uses request.user, and thus it makes no sense for the view to accept a username.


Note: You can limit views to a view to authenticated users with the
@login_required decorator [Django-doc].


Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.

Leave a comment