I have 2 classes defined in my models.py:
class Job(models.Model):
job_id = models.CharField(primary_key=True, max_length=10)
job_title = models.CharField(max_length=35)
min_salary = models.IntegerField(blank=True, null=True)
max_salary = models.IntegerField(blank=True, null=True)
class Meta:
managed = False
db_table = 'jobs'
def __str__(self):
return self.job_id
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
The issue is with foreign keys, on Employees admin page the Job field correctly displays drop-down with all possible job_ids, but the manager field displays drop-down with all entries equal "Employee object". I suspect this is because manager is a foreign key to self.employee
.
How do I fix that?
Thank you.
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments