Referencing model in another model is displaying id and object instead of name. How can I fix this?

I have made two models. One of which references another. Here is my first model: enter image description here

Here is my second model.py. enter image description here

(I know it is weird).

This is what I get.

enter image description here

I have tried using:

def __str__(self):
        return self.employee

Abd I have tried using:

def __str__(self):
            return self.employee.employee_name

I am not sure what I am doing wrong or how to fix this. I have also set employee = models.ForeignKey(Employee, on_delete=CASCADE) to see if that would make a difference. I knew it wouldn't. >:(

I honestly have never referenced another Model into another one before so I am a newbie at this. Any suggestions?

Any help would be greatly appreciated.

If you want to print employee name when printing Employee object (directly or via UserTiemsheet Object), you should override Employee class str method.

Sample Code

class Employee(models.Model):
    employee_name = models.CharField(max_length=100)
    
    def __str__(self):
    return self.employee_name

If you use 'superuser' account,then you can perform below steps provided by me. Now login using username and password now you can see 'Add Employee'. Enter the Employee Name and other Details then come back to the main webpage. Now you can view the Employee Name in place of 'Employee object1'.

  • images of my project. Before

The image is taken from my project as you can see first it was Artist(1) Now you can see the Name of the Artist

step-2 Click on the Employee object1 the if you can see the name and other details or else provide the details then click on save and refresh the page.Then you can see the Employee name.

Back to Top