Adding Foreign Key To Django Model

情到浓时终转凉″ 提交于 2019-12-07 18:30:26

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')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!