beda-software / drf-writable-nested

Writable nested model serializer for Django REST Framework
Other
1.07k stars 116 forks source link

how to use with self related field(manytomany) #168

Open flyly0755 opened 2 years ago

flyly0755 commented 2 years ago

I have one model with a field which is self related,

class zi(models.Model):
   """汉字数据模型"""
    chszi = models.CharField(max_length=1, null=False, blank=False, verbose_name="汉字", unique=True)
    relatedzi = models.ManyToManyField("self", null=True, blank=True, related_name="rzi",
                                       symmetrical=True, verbose_name='关联字')

class RelatedZiSerializer(serializers.ModelSerializer):
    """关联汉字的序列化"""
    class Meta:
        model = zi
        fields = ['id', 'chszi']

class ziDetialSerializer(WritableNestedModelSerializer, serializers.ModelSerializer):
    """汉字详情的序列化"""
    relatedzi = RelatedZiSerializer(many=True) 

with code above, send a put request to update it, but with error showed below:

image

how to solve this problem?