我的 views.py 如下所示:
@api_view(('GET',))
def save_progress_for_particular_user(request, exerciseId, rating):
#view to call stored proc that stores the progress for a particular user
#save_progress(user_id,exercise_id,rating,date)
result=Exercise_progress_model.save_progress_for_particular_user(
request.user.id, exerciseId, rating, datetime.date.today())
serializer = ExerciseProgressSerializer(result, many=True)
return Response(serializer.data)
我的models.py
样子如下:
class Exercise_progress_model(models.Model):
# Model that will be rendered on the UI
#Field for storing intensity level
INTENSITY_LEVEL_CHOICES = (
(1, 'Really simple'),
(2, 'Rather Simple'),
(3, 'Simple'),
(4, 'Okay'),
(5, 'Difficult'),
(6, 'Rather Difficult'),
(7, 'Really Difficult'),
)
intensity_level = models.IntegerField(choices=INTENSITY_LEVEL_CHOICES)
#Field for storing progress
progress = models.IntegerField(default='0')
#Field for storing exercise type
EXERCISE_TYPE_CHOICES = (
(1, 'Best stretch'),
(2, 'Butterfly reverse'),
(3, 'Squat row'),
(4, 'Plank'),
(5, 'Push up'),
(6, 'Side plank'),
(7, 'Squat'),
)
exercise_id = models.IntegerField(choices=EXERCISE_TYPE_CHOICES)
#Field for storing current date
current_date = models.DateField()
#Field for storing user rating
user_rating = models.IntegerField(default='0')
# static method to save progress for a particular user
@staticmethod
def save_progress_for_particular_user(user_id, exercise_id, rating, date):
# create a cursor
cur = connection.cursor()
# execute the stored procedure passing in
# search_string as a parameter
cur.callproc('save_exercise_state', [user_id, exercise_id, rating, date,])
# grab the results
result = cur.fetchall()
cur.close()
User_progress_list = []
for row in result:
Epm = Exercise_progress_model()
Epm.intensity_level=row[0]
Epm.progress=row[1]
Epm.exercise_id=row[2]
Epm.user_rating=row[3]
Epm.current_date=row[4]
User_progress_list.append(Epm)
return User_progress_list
我的serializers.py
样子如下:
class ExerciseProgressSerializer(serializers.Serializer):
intensity_level = serializers.IntegerField(required=True)
progress = serializers.IntegerField(required=True)
exercise_type = serializers.IntegerField(required=True)
user_rating = serializers.IntegerField(required=True)
current_date = serializers.DateField()
我的存储过程如下所示:
delimiter //
CREATE PROCEDURE save_exercise_state(
p_user_id int, p_exercise_id int, p_rating int, p_date date
)
BEGIN
DECLARE lv_current_intensity, lv_progress, lv_final_intensity int;
DECLARE lv_date date;
select intensity_level into lv_current_intensity
from demo_exercise_state where
user_id=p_user_id and exercise_id=p_exercise_id;
select progress into lv_progress
from demo_exercise_state where
user_id=p_user_id and exercise_id=p_exercise_id;
select current_date into lv_date
from demo_exercise_state where
user_id=p_user_id and exercise_id=p_exercise_id;
select lv_progress, lv_current_intensity, lv_date;
If lv_date=p_date then
select "OK";
end if;
if p_rating=0 then
set lv_progress=lv_progress;
elseif p_rating=1 then
if lv_progress=3 then
set lv_progress=0;
if lv_current_intensity = 7 then
set lv_final_intensity = 7;
else
set lv_final_intensity = lv_current_intensity + 1;
end if;
else
if lv_current_intensity=7 then
set lv_progress=0;
set lv_final_intensity=lv_current_intensity;
else
set lv_progress=lv_progress+1;
set lv_final_intensity=lv_current_intensity;
end if;
end if;
elseif p_rating =-1 then
if lv_progress=0 then
if lv_current_intensity = 1 then
set lv_final_intensity = 1;
set lv_progress=0;
else
set lv_final_intensity = lv_current_intensity - 1;
set lv_progress=3;
end if;
else
set lv_progress=lv_progress-1;
set lv_final_intensity=lv_current_intensity;
end if;
end if;
Update demo_exercise_state
SET progress = lv_progress,
intensity_level = lv_final_intensity,
`current_date` = p_date,
user_rating = p_rating
WHERE user_id = p_user_id AND exercise_id = p_exercise_id;
SELECT intensity_level, progress, exercise_id, user_rating , current_date
FROM demo_exercise_state
WHERE user_id = p_user_id AND exercise_id = p_exercise_id AND user_rating=p_rating;
END
//
调用 save_exercise_state(4,1,1,curdate());执行此 proc 的结果截图 如下
当我调用此视图时,我得到该行的元组索引超出范围错误 Epm.user_rating=row[3]
。
这是什么原因?有什么解决方法吗?