[Answered ]-Django shows object reference in foreign key column

2๐Ÿ‘

โœ…

You defined the __str__ and __unicode__ methods on your Employee.Meta class.

De-indent them four space and you should be good:

class Employee(models.Model):
    employee_id = models.IntegerField(primary_key=True)
    first_name = models.CharField(max_length=20, blank=True)
    last_name = models.CharField(max_length=25)
    email = models.CharField(unique=True, max_length=25)
    phone_number = models.CharField(max_length=20, blank=True)
    hire_date = models.DateField()
    job = models.ForeignKey('Job')
    salary = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
    commission_pct = models.DecimalField(max_digits=2, decimal_places=2, blank=True, null=True)
    manager = models.ForeignKey('self', blank=True, null=True, related_name ='employee')
    department_id = models.IntegerField(blank=True, null=True)
    class Meta:
        managed = False
        db_table = 'employees'

    def __str__(self):
       return self.employee_id
    def __unicode__(self):
       return self.employee_id
๐Ÿ‘คSimon Charette

0๐Ÿ‘

Add __unicode__() or __str__() method in your Employee model as you have done in Job.

class Employee(models.Model):
    def __unicode__(self):
        return self.email #or whatever string you want

This method is used to represent objects as string in django.

๐Ÿ‘คRohan

Leave a comment