[Answered ]-How to list objects that have the same ForeignKey attribute?

0πŸ‘

βœ…

To access the related objects ApprovalProcess to ApprovalSteps, you can use _set (Django Docs).

So in your case in you template approval_item.approval_id_set.all:

{% for item in approval_item.approval_id_set.all %}
    {{ item.doc_id }}
{% endfor %}

in views.py:

approval_item.approval_id_set.all()

Or use ForeignKey.related_name:

approval_id = models.ForeignKey(
    ApprovalProcess, on_delete=models.CASCADE, null=True, 
    related_name="processes"
)
# then you can use 
approval_item.processes.all()

Also I recommend you to rename approval_id to approval because:

Behind the scenes, Django appends "_id" to the field name to
create its database column name, see Django ForeignKey

πŸ‘€NKSM

1πŸ‘

You can change your approval_details view function to

def approval_details(request, pk):
    approval_items_list = get_list_or_404(ApprovalSteps, approval_id=pk)
    return render(request, 'approval_details.html', {'approval_items_list': approval_items_list})

Refer here

Leave a comment