DRF M2M嵌套序列化程序字段错误地键入为字符串

时间:2018-05-28 18:23:41

标签: django python-3.x django-models django-rest-framework graphene-python

每当我在创建TechnologyStack时尝试反序列化Technology对象列表时,我收到消息:

  

Field \" technologies \"类型\"字符串\"不得有子选择。

我使用djangorestframework == 3.8.2。任何人都知道造成这种情况的原因和/或潜在的解决方案吗?

另外,请注意我使用石墨烯-django库。但是,似乎这个问题可归因于drf序列化器。

class TechnologyProfile(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=100, unique=True)


class Technology(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    profile = models.ForeignKey(TechnologyProfile, blank=True, null=True, on_delete=models.CASCADE)

    class Meta:
        verbose_name_plural = "technologies"


class TechnologyStack(models.Model):
    technologies = models.ManyToManyField(Technology, related_name='technology_stacks')


class TechnologyProfileSerializer(serializers.ModelSerializer):
    user_id = RelayIdField()
    name = serializers.CharField()

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


class TechnologySerializer(serializers.ModelSerializer):
    user_id = RelayIdField()
    profile = TechnologyProfileSerializer(many=False)

    class Meta:
        model = Technology
        fields = '__all__'


class TechnologyStackSerializer(serializers.ModelSerializer):
    technologies = TechnologySerializer(many=True)

    class Meta:
        model = TechnologyStack
        read_only_fields = ('id',)
        fields = '__all__'

0 个答案:

没有答案