如何在django模型中设置外键?

时间:2013-08-27 08:35:07

标签: python mysql django

我想将app_id'的应用程序模型设置为interviewstable的app_id的外键

class application(models.Model):
    app_id = models.IntegerField(max_length=200)
    job_title = models.CharField(max_length=200)
    odesk_id = models.CharField(max_length=200)
    client_spent = models.CharField(max_length=200)
    job_type = models.CharField(max_length=200)
    notes_type = models.CharField(max_length=500)



class interviewtable(models.Model):
    app_id = models.IntegerField(max_length=200)
    interview = models.CharField(max_length=200)
    interview_on = models.CharField(max_length=200)
    interview_notes = models.CharField(max_length=200) 

1 个答案:

答案 0 :(得分:4)

像这样:

class interviewtable(models.Model):
    app = models.ForeignKey(application)
    interview = models.CharField(max_length=200)
    interview_on = models.CharField(max_length=200)
    interview_notes = models.CharField(max_length=200) 

Django自动添加id,因此app应为app_id

此外,您不希望在整数字段上使用max_length。如果您想要使用大整数BigIntegerField()请正确阅读文档:https://docs.djangoproject.com/en/1.5/