# replace id with other field in the views

**URL:** <https://forum.djangoproject.com/t/replace-id-with-other-field-in-the-views/14363>\
**Category:** Forms & APIs\
**Created:** [June 18, 2022, 5:52pm UTC](https://forum.djangoproject.com/t/replace-id-with-other-field-in-the-views/14363 "2022-06-18T17:52:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kingman29](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kingman29/32/8853_2.png) [@kingman29](https://forum.djangoproject.com/u/kingman29)\
**Post date:** [June 18, 2022, 5:52pm UTC](https://forum.djangoproject.com/t/replace-id-with-other-field-in-the-views/14363/1 "2022-06-18T17:52:28Z")

</div>

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:

 ![fabrication type id](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/8/8b0e0175c531396da9905fba67e5473e38d5551d.png)

the column type\_id is comming from the composant\_type class,

models.py:

```auto
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

```auto
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

---

<div class="post-metadata">

**Author:** ![kingman29](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kingman29/32/8853_2.png) [@kingman29](https://forum.djangoproject.com/u/kingman29)\
**Post date:** [June 18, 2022, 6:59pm UTC](https://forum.djangoproject.com/t/replace-id-with-other-field-in-the-views/14363/2 "2022-06-18T18:59:18Z")

</div>

this is the answer, thank you

```auto
def fabrications(request):
  lesfichestab = fiche.objects.all()
  fabricationtab = fabrication.objects.all().values()
  df = pd.DataFrame.from_records(
    fabricationtab.values(
      "grade",
      "type__name",
      "composant",
      "unite",
      "composant_value"))

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 18, 2022, 7:03pm UTC](https://forum.djangoproject.com/t/replace-id-with-other-field-in-the-views/14363/3 "2022-06-18T19:03:15Z")

</div>

In Django, instances of Models are instances of Python classes. Foreign Key attributes are class instance references.

Example: Assume two classes `Book` and `Author`, where `Book` has a field (“attribute”) named `author` being a ForeignKey to `Book`. If you have an instance of `book`, then `book.author` is a reference to that instance of `Author`. If `Author` has a field (“attribute”) named `name`, then `book.author.name` is the name of the author of that book.

Review the docs at [Templates | Django documentation | Django](https://docs.djangoproject.com/en/4.0/topics/templates/#the-django-template-language), [Model field reference | Django documentation | Django](https://docs.djangoproject.com/en/4.0/ref/models/fields/#foreignkey) and [Making queries | Django documentation | Django](https://docs.djangoproject.com/en/4.0/topics/db/queries/)

As a side note, I’ll mention that if there’s any “filtering” of the data that needs to be done, you’re better off doing it by writing your query to filter the data _before_ you start processing it. You may also find it more perfomant to construct your query such that it returns the data in its “pivoted” format rather than passing it through to Pandas.

---

<div class="post-metadata">

**Author:** ![kingman29](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kingman29/32/8853_2.png) [@kingman29](https://forum.djangoproject.com/u/kingman29)\
**Post date:** [June 18, 2022, 7:25pm UTC](https://forum.djangoproject.com/t/replace-id-with-other-field-in-the-views/14363/6 "2022-06-18T19:25:50Z")

</div>

Hello Mr Ken  
thank you very much, you gave a good lesson yesterday,  
I tried to change the id column to another field, and export the database using ImportExport mothod, I saw that this operation gave me an Excel table with Id’s and not with name field (in my cas I want to change id with name field), so I understand what you sad,  
and now, you gave the exact documentation of my second question : what If I didn"t use Pandas, how can I tell Django to change id with other Field ? so you give the answer

I make me feel very happy, I wish become like you
