How to access other data of foreign key and call them on frontend in django

I have to models Customers and Purchase_order. I am taking customer as a foreign key in purchase invoice and fetching data..

My data is like this:

    {'id': 5, 'purchase_number': 'TES-PO-1', 'purchase_date':, 9, 1), 'customer_id_id': 1, 'special_instructions': '', 'total': '70', 'discount': 2.578125, 'round_off': '77.5', 'grand_total': '78', 'user_id': 1, 'remarks': '', 'company_id': 1}
 {'id': 6, 'purchase_number': 'TES-PO-2', 'purchase_date':, 9, 6), 'customer_id_id': 2, 'special_instructions': '', 'total': '24', 'discount': 0.75, 'round_off': '28.5', 'grand_total': '29', 'user_id': 1, 'remarks': '', 'company_id': 1}

here I am getting customer id as 1,2,3 its pk value but I wish to call its customer_company there.. how can I access them by its id on frontend?

want to show name instead of ID

enter image description here

you can get the customer object customer = Customer.objects.get(pk=customer_ID) then access the name field of Customer model by name =

==== I have two models In AddmitatonModel has foreign key of StudentModel ====

class StudentModel(models.Model):
    name = models.CharField(max_length=255)
    roll = models.PositiveIntegerField()
    email = models.EmailField()
    city = models.CharField(max_length=255)

    def __str__(self):

class AddmissionModel(models.Model):
    student = models.ForeignKey(StudentModel,on_delete=models.CASCADE)
    status = models.BooleanField(default=False)

    def __str__(self):
        return str(

======== in =========

def DemoView(request):
    all_students = AddmissionModel.objects.all()
    context = {'all_students':all_students}
    return render(request,'demo.html',context)

====== in html file itrated all student detail using foreigenkey ======

            <tr style="border-bottom: 1px solid;">
                <th>Student name</th>
                <th>Student Roll</th>
                <th>Student Email</th>
                <th>Student City</th>


            {% for i in all_students %}
            {% endfor %}

======== Webpage output =============

enter image description here

This worked on me

@login_required(login_url='/login') def get_sales_invoices(request): company_label= Company_Setup.objects.get(id=request.session['company_id']) invoices=Sales_Invoice.objects.filter(company=company_label).all()

invoice_list = [[i.customer_id.customer_company, i.invoice_number, i.invoice_date, i.grand_total,] for i in invoices]
response = invoice_list

return render(request,"sales/list_sales_invoice.html",{'invoices':invoices, 'customer_data':response})


<tbody id="rows">
                            {% if customer_data %}
                            {% for i in customer_data %}
                                <td><a class='bx bxs-edit' style="text-decoration:None"
                                    <a class="fas fa-eye" href="/sales/sales-invoice-pdf/{{i.4}}">&nbsp;&nbsp;
                                    <a href="/sales/delete/{{i.4}}" class="bx bxs-trash-alt" href=""></a>
                            {% endfor %}
                            {% endif %}
Back to Top