# Change the foreignKey id to another Field

**URL:** <https://forum.djangoproject.com/t/change-the-foreignkey-id-to-another-field/14344>\
**Category:** Using the ORM\
**Created:** [June 17, 2022, 9:22pm UTC](https://forum.djangoproject.com/t/change-the-foreignkey-id-to-another-field/14344 "2022-06-17T21:22:23Z")\
**Posts on this page:** 5\
**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 17, 2022, 9:22pm UTC](https://forum.djangoproject.com/t/change-the-foreignkey-id-to-another-field/14344/1 "2022-06-17T21:22:23Z")

</div>

I don’t know if there is a logic for this, because i seen it before when I programed with C# and VB, but in Django, it’s seems hard to get it.

So what I want to ? I want the table in database save the name and not the id of the foreignKey. And this is the model.py

```auto
from pyexpat import model
from statistics import mode
from venv import create
from django.db import models
from django.contrib.auth.models import User
from django.db.models.base import Model
from django.forms import IntegerField

class Post(models.Model):
    name = models.CharField(max_length=150)

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

class Person(models.Model):
    post = models.ForeignKey(Post , on_delete=models.CASCADE)
    name = models.CharField(max_length=100, unique=True)

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

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

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

class Classification(models.Model):
    name = models.ForeignKey(Person, on_delete=models.CASCADE)
    room = models.ForeignKey(Room, on_delete=models.CASCADE)
    date = models.DateField(auto_created=False)
    create_date = models.DateTimeField(auto_created=True)

    def __str__ (self):
        return f"({self.name} Take {self.room} at {self.date})"
        

```

The result in Database table:

![id problem 2](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/7/700ca8a6d1fbd6ad77fe88911674f81a3bf437d6.png)

What I want to see in the database table, is that Django replace the id’s in name\_id column with names, and room\_id with room names

is that possible ?

Thank you

---

<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 17, 2022, 9:38pm UTC](https://forum.djangoproject.com/t/change-the-foreignkey-id-to-another-field/14344/2 "2022-06-17T21:38:15Z")

</div>

See [Model field reference | Django documentation | Django](https://docs.djangoproject.com/en/4.0/ref/models/fields/#django.db.models.ForeignKey.to_field)  
(Usually a bad idea in most cases, but Django won’t prevent you from making some mistakes.)

---

<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 17, 2022, 9:41pm UTC](https://forum.djangoproject.com/t/change-the-foreignkey-id-to-another-field/14344/3 "2022-06-17T21:41:59Z")

</div>

Do you mean Django preferd id then other field ?

---

<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 17, 2022, 9:46pm UTC](https://forum.djangoproject.com/t/change-the-foreignkey-id-to-another-field/14344/4 "2022-06-17T21:46:02Z")

</div>

As a general rule in database design, you want your foreign keys to reference the primary key of the object table. (This isn’t something that is specific to Django, it’s a principle of good relational data modelling.) That primary key is the identifier of that row, allowing for all other fields to be changed. By assigning a Foreign Key to a different field, you are, in effect, creating two separate identifiers for that row.

If you don’t want numeric IDs as the primary key, that’s acceptable as well - but then you still end up having the Foreign Key referencing the primary key of the target table.

---

<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 17, 2022, 9:53pm UTC](https://forum.djangoproject.com/t/change-the-foreignkey-id-to-another-field/14344/5 "2022-06-17T21:53:38Z")

</div>

Thank you verymuch for this,  
All what I want that when using Pandas filtering, it’s show me names not ID’s , this is why I want to names shoing in the table, so when I filtering with Pandas, names showed and not ID’s  
I was have an Idea, that Set the table with id’s, and trying to doing a lookup Method for each column of id’s (example, in Classification, in the room\_id column , triung to lookup the ID’s with their room name, after that set Pandas Filtering)
