Hello,
my question is:
How do I tell to Django to replace the Column type_id to the name field in the views (html page).
and here I have foreignkey, it gave me id (type_id), and this screentshot of fabrication class:
the column type_id is comming from the composant_type class,
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 get_object_or_404, 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)
note : I use Pandas method, because i have to do some Feltring and pivoting of the table.
thank you