[Answered ]-Django getting data from database using foreign key

1👍

You fetch the Posts with:

posts = Post.objects.filter(status=True)

but it makes no sense to write:

posts.author

since posts is a QuerySet (so a collection) of Posts, not a single one.

You can enumerate over the posts and then show the .authors for all Post objects:

for post in posts:
    print(post.author.about_user)

or in the template with:

{% for post in posts %}
    {{ post.author.about_user }}
{% endfor %}

You can boost efficiency by fetching all the Users in the same query with:

posts = Post.objects.filter(status=True).select_related('author')

Leave a comment