Django Rest Framework ModelSerializer保存错误

时间:2016-06-09 01:10:20

标签: python django django-rest-framework

我想从传递给序列化程序的数据中添加一个额外的键值对,但是会发生以下错误:

ValueError: Cannot assign "1": "ExamLog.student" must be a "Students" instance.

serializers.py

class ExamSubmitSerializer(serializers.ModelSerializer):
    def to_internal_value(self, data):
        x = get_score(data['exam']) # This will result to 1, I have no errors with this one
        data['score'] = x 
        return data

    class Meta:
        model = ExamLog
        fields = ('student', 'score', 'exam_started')

    # Will use this one for auto-email and auto-pdf creation
    # def create(self, validated_data):
    #   # print "-----"
    #   # y = validated_data.pop('exam', None)
    #   # print y
    #   print validated_data
        # x = self.Meta.model(**validated_data)
        # x.save()

views.py

@api_view(['POST'])
def submitExam(request):
    if request.method == 'POST':
        data = request.data
        student = data.get('student', None)
        exam = data.get('exam', None)
        serializer = ExamSubmitSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return Response(standardResponse(data=serializer.data), status=status.HTTP_201_CREATED)
        else:
            return Response(standardResponse(errors=serializer.errors), status=status.HTTP_400_BAD_REQUEST)

urls.py

urlpatterns = [
  # url(r'^view/', views.viewExams),
  url(r'^view/', views.getQuestion),
  url(r'^add/', views.addUpdateQuestion),
  url(r'^submit/', views.submitExam),
  url(r'^', views.filterQuestions),
]

models.py

class Choices(models.Model):
    question = models.ForeignKey(Questions)
    choice = models.TextField()
    correct = models.NullBooleanField()

    def __unicode__(self):
        return self.choice

class ExamLog(models.Model):
    student = models.ForeignKey(Students)
    # answers = models.JSONField? # To be used for data science statistical purposes
    score = models.PositiveIntegerField(null=True, blank=True, default=None)
    exam_started = models.DateTimeField(null=True, blank=True, default=None)
    exam_finished = models.DateTimeField(auto_now_add=True)

get_score方法:

def get_score(answers):
    x  = Choices.objects.filter(id__in=answers).filter(correct=1)
    return x.count()

API请求示例:

{"student":1, "exam":[ 1597, 1620, 1906, 1648, 1656, 1718, 1611, 1551, 1812, 1745, 1489, 1815, 1527, 1485, 1512, 1831, 1545, 1713, 1808, 1530 ]}

我理解错误的含义。应该传递模型的实例而不是其id,但是当我不包含 to_internal_value()方法时,不会发生此错误并实际保存。但是,这将导致分数为空

我想在分数上添加一个值,该值将来自数据的考试

1 个答案:

答案 0 :(得分:3)

def to_internal_value(self, data):
    x = get_score(data['exam']) # This will result to 1, I have no errors with this one
    data['score'] = x

    data = super(ExamSubmitSerializer, self).to_internal_value(data)

    return data

在返回数据之前调用super方法将允许再次清理数据以及操纵得分数据键值。