Как получить доступ к другим данным внешнего ключа и вызвать их на фронтенде в django

Мне нужно моделировать Customers и Purchase_order. Я беру клиента как внешний ключ в счете на покупку и получаю данные...

Мои данные выглядят следующим образом:

    {'id': 5, 'purchase_number': 'TES-PO-1', 'purchase_date': datetime.date(2022, 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': datetime.date(2022, 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}
    Action

здесь я получаю id клиента как 1,2,3 его pk значение, но я хочу вызвать его customer_company там... как я могу получить доступ к ним по их id на frontend?

хотите показывать имя вместо ID

enter image description here

вы можете получить объект customer customer = Customer.objects.get(pk=customer_ID) затем получить доступ к полю name модели Customer по name = customer.name

==== У меня есть две модели В AddmitatonModel есть внешний ключ 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):
        return self.name


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

    def __str__(self):
        return str(self.student.name)

======== в view.py =========

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

====== в html-файле проверил все данные студента, используя foreigenkey ======

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

        <tbody>

            {% for i in all_students %}
            <tr>
                <td>{{forloop.counter}}</td>
                <td>{{i.student.name}}</td>
                <td>{{i.student.roll}}</td>
                <td>{{i.student.email}}</td>
                <td>{{i.student.city}}</td>
                <td>{{i.status}}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>

======== Вывод веб-страницы =============

enter image description here

У меня сработало вот это views.py

@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,i.id] for i in invoices]
response = invoice_list


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

html

<tbody id="rows">
                            {% if customer_data %}
                            {% for i in customer_data %}
                           
                            <tr>
                                <td><a class='bx bxs-edit' style="text-decoration:None"
                                        href="/sales/update/{{i.4}}"></a>&nbsp;&nbsp;
                                    <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>
                                </td>
                                <th>{{i.2}}</th>
                                <th>{{i.1}}</th>
                                <th>{{i.0}}</th>
                                <th>{{i.3}}</th>
                            </tr>
                            {% endfor %}
                            {% endif %}
                        </tbody>
Вернуться на верх