Django REST序列化程序错误

时间:2018-05-09 10:19:14

标签: python django django-rest-framework serializer

我正在定义模型序列化程序以在Django REST中POST一个任务实例。

我的模特是:

class Task(models.Model):
    """
    Model instance represents basic info about calculations doing in Celery worker.
    """
    id = models.AutoField(primary_key=True)
    name = models.CharField(blank=True, max_length=100)
    description = models.CharField(blank=True, max_length=300)

class Vector(models.Model):
    """
    Model instance represents geometry file (*.SHP) saved in FileSystem.
    """
    id = models.AutoField(primary_key=True)
    name = models.CharField()
    file_path = models.CharField()

class CalculateVector(models.Model):
    """
    Model instance represents neccessary info for call Celery worker task.
    It contains 1:M relation between Vector and CalculateVector. 
    """
    id = models.AutoField(primary_key=True)
    task_id = models.ForeignKey(Task, related_name='calculate_vectors', on_delete=models.CASCADE)
    method = models.CharField()
    vector = models.ForeignKey(Vector, models.SET_NULL, related_name='vector')

因此每个CalculateVector实例只有一个Vector模型实例的信息,但Vector模型可以包含多个CalculateVector实例。

我的序列化器是:

class VectorSerializer(serializers.ModelSerializer):

    id = serializers.IntegerField()

    class Meta:
        model = Vector
        fields = (
            'id',
            'name'
        )


class CalculateVectorSerializer(serializers.ModelSerializer):

    vector = VectorSerializer()

    class Meta:
        model = CalculateVector
        fields = (
            'id',
            'method',
            'vector'
        )

class CreateTaskSerializer(serializers.ModelSerializer):

    calculate_vectors = CalculateVectorSerializer()

    class Meta:
    model = Task
    fields = (
        'id',
        'name',
        'description',
        'calculate_vectors'
    )

    def create(self, validated_data):

        calculate_vector_data = validated_data.pop('calculate_vectors')
        instance = Task.objects.create(**validated_data)

        vector = Vector.objects.get(id=calculate_vector_data.get('vector')['id'])
        CalculateVector.objects.update_or_create(
            task_id=instance,
            method=calculate_vector_data['method'],
            vector=vector,
        )

        return instance

尝试查询此序列化程序时,我收到错误:

>>> data = {'name':'test task','description':'description','calculate_vectors':{'vector':{'id':1,'name':'calc1'},'method':'method1'}}
>>> serializer = CreateTaskSerializer(data=data)
>>> serializer.is_valid()
>>> serializer.save()
>>> serializer.data

AttributeError: Got AttributeError when attempting to get a value for field `vector` on serializer `CalculateVectorSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `RelatedManager` instance.
Original exception text was: 'RelatedManager' object has no attribute 'vector'.

为什么我不能使用serializer.data变量?我该如何解决这个问题?有什么帮助吗?

1 个答案:

答案 0 :(得分:2)

calculate_vectorsRelatedManager并返回对象列表,您应该将many=True参数添加到CreateTaskSerializer中:

class CreateTaskSerializer(serializers.ModelSerializer):

    calculate_vectors = CalculateVectorSerializer(many=True)

同样在create()方法validated_data.pop('calculate_vectors')将返回列表,因此您需要迭代它:

 def create(self, validated_data):

    calculate_vector_data = validated_data.pop('calculate_vectors')
    instance = Task.objects.create(**validated_data)
    for vector_data in calculate_vector_data:    
        vector = Vector.objects.get(id=vector_data.get('vector')['id'])
        CalculateVector.objects.update_or_create(
        task_id=instance,
        method=vector_data['method'],
        vector=vector,
        )

当您发布数据时,它也应该以JSON列出:

"calculate_vectors": [{"somedata"}, {"somedata"}]