我的项目中有教师和对话模型,还有一个rate函数,它是一个POST API,以teacherId和rate作为输入。教师将电话字段作为ID。调用rate函数时,教师的rate和raters字段以及对话的isRated字段必须更改。
以下是我的对话模式:
class Conversation(models.Model):
isRated = models.BooleanField(null=False, default=False)
class TeacherUsers(models.Model):
phone = models.IntegerField(unique=True, null=False, default=0)
rate = models.FloatField(null=True, default=0)
raters = models.IntegerField(unique=True, null=False, default=0)
class RateSerializer(serializers.Serializer):
teacherId = serializers.IntegerField(required=True)
rate = serializers.IntegerField(required=False)
def update(self, instance, validated_data):
instance.rate = validated_data['rate']
instance.raters = validated_data['raters']
instance.save()
return instance