序列化器未将数据保存到模型

时间:2019-08-30 13:58:40

标签: python django

目标:

使用令牌获取教师对象,然后将<?php $sql = "SELECT COUNT(*) FROM fruit WHERE calories > 100"; if ($res = $conn->query($sql)) { /* Check the number of rows that match the SELECT statement */ if ($res->fetchColumn() > 0) { /* Issue the real SELECT statement and work with the results */ $sql = "SELECT name FROM fruit WHERE calories > 100"; foreach ($conn->query($sql) as $row) { print "Name: " . $row['NAME'] . "\n"; } } /* No rows matched -- do something else */ else { print "No rows matched the query."; } } $res = null; $conn = null; ?> 与教师对象一起插入模型中,因为模型需要外键的教师对象。

views.py

request.data

serializers.py

def put(self, request, *args, **kwargs):
    token = '0c023159d66477d15f389ed9c951fe0f29e4bb81'
    instance = self.get_object(token)
    serializer = NoticeBoardSerializer(instance,data=request.data)
    serializer.is_valid(raise_exception=True)
    serializer.save()
    return Response(serializer.data)

def get_object(self, token):
    user = Token.objects.get(key=token).user
    if user:
        teacher = Teacher.objects.get(teacher__username = user)
        return teacher
    else:
        return Response({"error":"error"})

0 个答案:

没有答案