Localhost Screenshot 次ている「運動」のインスタンスである必要があります。Djangoは割り当てることができません、「Rating.exerciseは、」私は要件
- は演習がありますし、
- 運動は、複数の評価 を持つことができる評価があります
私の目標は、エクササイズを動的に追加することです。つまり、前もってすべてのエクササイズを作成し、特定のエクササイズに格付けしました。私はビュー「をsave_rating 」を打つとき、私はエラーを取得[]「は割り当てることができません 『』: 『Rating.exercise演習』でなければならない 『
私が間違って何をやっている』インスタンスは。」?
class RatingSerializer(serializers.Serializer):
class Meta:
model = Rating
fields = ('value')
class ExerciseSerializer(serializers.Serializer):
pk = serializers.IntegerField(read_only=True)
exercise_type = serializers.CharField(required=True, allow_blank=False)
intensity_level = serializers.IntegerField(required=True)
video_url = serializers.URLField(required=True)
description = serializers.CharField(required=True, allow_blank=False)
ratings = RatingSerializer(many=True, read_only=True)
マイビュー:
マイmodels.pyが
class Exercise(models.Model):
#Field for storing exercise type
EXERCISE_TYPE_CHOICES = (
('BS', 'Best stretch'),
('BR', 'Butterfly reverse'),
('SR', 'Squat row'),
('PL', 'Plank'),
('PU', 'Push up'),
('SP', 'Side plank'),
('SQ', 'Squat'),
)
exercise_type = models.CharField(max_length=5,choices=EXERCISE_TYPE_CHOICES)
#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 video url for a particular exercise
video_url = models.URLField()
#Field for storing description of the exercise
description = models.CharField(max_length=500)
class Rating(models.Model):
#Field for storing exercise type
exercise = models.ForeignKey(Exercise, related_name='ratings', blank=True, null=True)
#Field for storing rating
RATING_CHOICES = (
('H', 'Happy'),
('N', 'Neutral'),
('S', 'Sad'),
)
value = models.CharField(max_length=1,choices=RATING_CHOICES)
を次のようになります。私は、次のように私のシリアライザを定義しているあなたの関数save_rating
で
def exercise_list(request):
"""
List all exercises.
"""
if request.method == 'GET':
exercises = Exercise.objects.filter(exercise_type='BS', intensity_level=1)
serializer = ExerciseSerializer(exercises, many=True)
return JSONResponse(serializer.data)
def save_rating(request):
"""
Save a rating for a specific exercise.
"""
#Get a specific exercise for which you want to save the rating
specificExercise = Exercise.objects.filter(exercise_type='BS' , intensity_level=1)
#Create a rating and pass the specific exercise reference to it
Rating.objects.create(exercise = specificExercise, value='H')
#serializer = ExerciseSerializer(rating, many=True)
serializer = ExerciseSerializer(instance=specificExercise)
return JSONResponse(serializer.data)
修正方法について教えていただけますか?私は比較的新しいDjangoです。 行を に変更すると、エクササイズ= Exercise.objects.get(exercise_type = 'BS'、intensity_level = 1)に2つの空白のレーティングが作成されますが、レーティングの値、つまり 'H'は挿入されません。 – Nitish
私は私の答えを編集しました。しかし、私はあなたのコードがなぜ機能しないのか理解してほしい。 djangoのドキュメントをよく読んで、それが意味をなさないかどうかを確認してください:https://docs.djangoproject.com/ja/1.9/topics/db/queries/#retrieving-a-single-object-with-get –
確かに!あなたの答えはうまくいった、それだけで保存された評価は空白です。 Rating.objects.create(exercise = specificExercise、value = 'H')、値 'H'は挿入されません。 – Nitish