Brar Kirmal
Brar Kirmal

Reputation: 139

how to set a foreign key in django model?

I want to set a 'app_id'of application model as a foreign key for interviewstable's 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) 

Upvotes: 1

Views: 3153

Answers (1)

Joyfulgrind
Joyfulgrind

Reputation: 2836

Like this:

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 with automatically add id thus app would be app_id.

Also, you don't want to use max_length on integer field. If you want big integer use BigIntegerField() Read the documentation properly: https://docs.djangoproject.com/en/1.5/

Upvotes: 4

Related Questions