添加外键到Django模型 [英] Adding Foreign Key To Django Model
问题描述
我想知道在将列从IntegerField转换为外键的最佳过程中。这是我的两个模型:
类锻炼(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)
和第二个:
类运动(models.Model):
userid = models.IntegerField ();
workoutid = models.IntegerField();
exercisename = models.CharField(max_length = 100)
repetitions = models.IntegerField()
weight = models.FloatField()
最后我的django视图代码:
user = User。 object.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])$ b $ b all_exercises = exercise.objects.filter(workoutid__in = workout_ids)
我想将练习模型中的workoutid列更改为与锻炼模型中的id字段相关的外键。我试过这个改变模型领域:
workoutid = models.ForeignKey(锻炼);
然后将这个order_by语句添加到我的视图中的all_exercises结尾,即
all_exercises = exercise.objects.filter(workoutid__in = workout_ids).order_by(workoutid__workoutdate)
/ pre>
我收到一条错误,说:
(1054 ,现场列表中的未知列tracking_exercise.workoutid_id)
我认为这是一个我的模型代码中的问题,但我不知道这可能是一个问题的视图。
感谢您的帮助!
解决方案您收到错误,因为workout_ids不是ids列表。
对于您当前的问题,快速简单的修复是使用
.values_list('id',flat = True)
像这样:workout_ids = workout.objects.filter(userid = user.id).\
filter(workoutdate__gte = dates [0])。 \\
过滤器(workoutdate__lte = dates [1])。\
values_list('id',flat = True)
但是,我强烈建议使用
models.ForeignKey
。这是一个简单的例子,让你开始:class Workout:
user = models.ForeignKey(User)
#其他字段去这里...
类练习:
workout = models.ForeignKey(锻炼)
#其他字段去这里...
然后你可以编写查询:
code> workout_list = Workout.objects.filter(user = request.user)
exercise_list = Exercise.objects.filter(workout__in = workout_list).order_by('workout__workoutdate')
I was wondering what they best procedures in turning a column from an IntegerField to a Foreign Key. Here are my two models:
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)
and the second one:
class exercise(models.Model): userid = models.IntegerField(); workoutid = models.IntegerField(); exercisename = models.CharField(max_length = 100) repetitions = models.IntegerField() weight = models.FloatField()
and finally my django view 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)
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:
workoutid = models.ForeignKey(workout);
and then add this this order_by statement to the end of all_exercises in my view i.e
all_exercises = exercise.objects.filter(workoutid__in = workout_ids).order_by("workoutid__workoutdate")
i get an error saying that:
(1054, "Unknown column 'tracking_exercise.workoutid_id' in 'field list'")
I think it is a problem in my Model code, but i'm not sure possibly it is an issue with the view.
Thanks for the help guys!
解决方案You are getting the error because workout_ids isn't a list of ids.
A quick and simple fix to your current problem is to use
.values_list('id', flat=True)
like so:workout_ids = workout.objects.filter(userid=user.id).\ filter(workoutdate__gte=dates[0]).\ filter(workoutdate__lte=dates[1]).\ values_list('id', flat=True)
However, I would highly recommend using
models.ForeignKey
. Here's a simple example to get you started:class Workout: user = models.ForeignKey(User) # other fields go here... class Exercise: workout = models.ForeignKey(Workout) # other fields go here...
Then you can write the query:
workout_list = Workout.objects.filter(user=request.user) exercise_list = Exercise.objects.filter(workout__in=workout_list).order_by('workout__workoutdate')
这篇关于添加外键到Django模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!