Django: Set foreign key using integer?

Posted by User on Stack Overflow See other posts from Stack Overflow or by User
Published on 2010-05-17T00:09:29Z Indexed on 2010/05/17 3:50 UTC
Read the original article Hit count: 230

Is there a way to set foreign key relationship using the integer id of a model? This would be for optimization purposes.

For example, suppose I have an Employee model:

class Employee(models.Model):
  first_name = models.CharField(max_length=100)
  last_name = models.CharField(max_length=100)
  type = models.ForeignKey('EmployeeType')

and

EmployeeType(models.Model):
  type = models.CharField(max_length=100)

I want the flexibility of having unlimited employee types, but in the deployed application there will likely be only a single type so I'm wondering if there is a way to hardcode the id and set the relationship this way. This way I can avoid a db call to get the EmployeeType object first.

© Stack Overflow or respective owner

Related posts about django

Related posts about django-models