我想知道将列从 IntegerField 转换为外键的最佳程序是什么。这是我的两个模型:
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)
第二个:
class exercise(models.Model):
userid = models.IntegerField();
workoutid = models.IntegerField();
exercisename = models.CharField(max_length = 100)
repetitions = models.IntegerField()
weight = models.FloatField()
最后是我的 Django 查看代码:
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)
我想将锻炼模型中的“workoutid”列更改为与锻炼模型中的 id 字段相关的外键。我尝试将更改模型字段更改为:
workoutid = models.ForeignKey(workout);
然后在我看来,将此 order_by 语句添加到 all_exercises 的末尾,即
all_exercises = exercise.objects.filter(workoutid__in = workout_ids).order_by("workoutid__workoutdate")
我收到一条错误消息:
(1054, "Unknown column 'tracking_exercise.workoutid_id' in 'field list'")
我认为这是我的模型代码中的一个问题,但我不确定这可能是视图的问题。
谢谢你们的帮助!