python - 如何在 Django 模型中设置外键?

标签 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) 

最佳答案

像这样:

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_id

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

关于python - 如何在 Django 模型中设置外键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18460701/

相关文章:

Python "++"运算符不起作用

php - 在钛中将图像上传到服务器时无法读取 php 文件

mysql - 错误代码 1292 - 截断不正确的 DOUBLE 值 - Mysql

mysql - WARN SqlExceptionHelper :143 - SQL Error: 0, SQLState: 08S01- SqlExceptionHelper:144 - 通信链接失败

python - 从字符串中获取后如何在paramiko中包含私钥?

python - Hyperopt 与 Spark MlLib 集成

python - Macbook 上的 scrapy 错误 : Module 'tutorial' already exists

python - 在父类中访问子类变量

django - 在 Crispy Forms 表单集中使用 HTML

python - 访问 Pandas 中的组