Django : замена id другим полем на странице представлений

Как мне сказать Django, чтобы он заменил Column type_id на поле name в представлениях (html-страница).

и здесь у меня есть foreignkey, он дал мне id (type_id), и этот скриншот класса производства:

enter image description here столбец type_id берется из класса composant_type,

models.py:

from django.db import models
from django.contrib.auth.models import User
from django.db.models.base import Model
from CentreCout.models import CentreCoutDB




class fiche(models.Model):
    centre_cout = models.CharField(max_length=150)
    number = models.CharField(max_length=100)
    name = models.CharField(max_length=100, unique=True)

    def __str__(self):
        return self.name

class unite(models.Model):
    name = models.CharField(max_length= 150, unique=True)

    def __str__(self):
        return self.name


class composant_type(models.Model):
    name = models.CharField(max_length=150, unique=True )

    def __str__(self):
        return f"({self.name})"


class composant_fab(models.Model):
    type = models.ForeignKey(composant_type, to_field='name',  on_delete=models.CASCADE)
    name = models.CharField(max_length=150, unique=True)

    def __str__(self):
        return f"({self.name})"
    


class fabrication(models.Model):
    grade = models.ForeignKey(fiche, to_field='name',on_delete=models.CASCADE)
    type = models.ForeignKey(composant_type, on_delete=models.CASCADE, blank=True, null=True)
    composant = models.ForeignKey(composant_fab , to_field='name', on_delete=models.CASCADE, null=True, blank=True)
    unite = models.ForeignKey(unite, to_field='name',on_delete=models.CASCADE)
    composant_value = models.FloatField(blank=True, null=True)

    def __str__(self):
        return f"({self.grade}-{self.composant}-{self.composant_value}-{self.unite})"

views.py

from django.shortcuts import render
from django import views
from django.http import HttpResponse
from .models import *
import pandas as pd



def fabrications(request):
  lesfichestab = fiche.objects.all()
  fabricationtab = fabrication.objects.all().values()
  df = pd.DataFrame(fabricationtab) 
  context = {
    'lesfichestab':lesfichestab,
    'fabricationtab':df.to_html()
  }

  return render(request,'fabrications/fabricationpage.html', context)

Примечание: Я использую метод Pandas, потому что мне нужно сделать некоторую фильтрацию и разворот таблицы.

Я получил ответ отсюда : Django с Pandas, обращающимся к ForeignKey

admin_data = pd.DataFrame.from_records(
administrations.values(
    "id",
    "study__name",  # <-- get the name through the foreign key
    "url_hash",
)
).rename(
    columns={
        "id": "administration_id",
        "study__name": "study_name",
        "url_hash": "link",
    }
)
Вернуться на верх