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
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:
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