title: Adding Foreign Key To Django Model
body: <p>I was wondering what they best procedures in turning a column from an IntegerField to a Foreign Key. Here are my two models: </p>
  
  <pre><code>class workout(models.Model):
  
      userid = models.IntegerField()
      datesubmitted = models.DateField()
      workoutdate = models.DateField(); 
      bodyweight = models.FloatField(null=True);
      totalreps = models.IntegerField() 
      totalweight = models.FloatField()
      numsets = models.IntegerField(); 
      numexercises = models.IntegerField()
      workoutname = models.CharField(max_length=250)
  </code></pre>
  
  <p>and the second one: </p>
  
  <pre><code>class exercise(models.Model):  
      userid = models.IntegerField();
      workoutid = models.IntegerField(); 
      exercisename = models.CharField(max_length = 100)
      repetitions = models.IntegerField()
      weight = models.FloatField()
  </code></pre>
  
  <p>and finally my django view code: </p>
  
  <pre><code>user =User.objects.get(username = request.session['username']) #user 
          dates = request.GET.getlist('datepickerfilter') # two dates
          workout_ids = workout.objects.filter(userid=user.id).filter( workoutdate__gte = dates[0]).filter(workoutdate__lte = dates[1])
          all_exercises = exercise.objects.filter(workoutid__in = workout_ids)
  </code></pre>
  
  <p>I want to change the 'workoutid' column in the exercise model to a foreign key that relates to the id field in the workout model. I tried this changing the changing the model field to: </p>
  
  <pre><code>workoutid = models.ForeignKey(workout);
  </code></pre>
  
  <p>and then add this this order_by statement to the end of all_exercises in my view i.e </p>
  
  <pre><code>all_exercises = exercise.objects.filter(workoutid__in = workout_ids).order_by("workoutid__workoutdate")
  </code></pre>
  
  <p>i get an error saying that:</p>
  
  <pre><code>(1054, "Unknown column 'tracking_exercise.workoutid_id' in 'field list'")
  </code></pre>
  
  <p>I think it is a problem in my Model code, but i'm not sure possibly it is an issue with the view. </p>
  
  <p>Thanks for the help guys! </p>