How do i save many to many fields objects using django rest framework
<p>I have three models Blogs, Posted, Tags. In Blogs model I have fields 'postedin' as foreign key to Posted model and 'tags' as manytomany fields to Tags model.</p>

<p>models.py:</p>

<pre><code>class Posted(models.Model):
    name = models.CharField(_('Posted In'),max_length=255, unique=True)

class Tags(models.Model):
    name = models.CharField(_('Tag Name'),max_length=255, unique=True)

class Blogs(models.Model):
    author = models.ForeignKey(CustomUser)
    title=models.CharField(max_length=100)
    postedin=models.ForeignKey(Posted)
    tags= models.ManyToManyField(Tags)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
</code></pre>

<p>views.py:</p>

<pre><code>class BlogViewSet(viewsets.ModelViewSet):
    queryset=Blogs.objects.order_by('-created_at')
    serializer_class= BlogsSerializer

def get_permissions(self):
    if self.request.method in permissions.SAFE_METHODS:
        return (permissions.AllowAny(),)
    return (permissions.IsAuthenticated(),IsAuthorOfBlog())

def perform_create(self,serializer):

    serializer.save(author=self.request.user)

    return super(BlogViewSet,self).perform_create(serializer)
</code></pre>

<p>serializers.py:</p>

<pre><code>class TagsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tags

        fields = ('pk','name')
        read_only_fields=('pk','name')

class PostedSerializer(serializers.ModelSerializer):
    class Meta:
        model = Posted

        fields = ('pk','name')
        read_only_fields=('pk','name')

class BlogsSerializer(serializers.ModelSerializer):
    author = AccountSerializer(read_only=True,required=False)
    tags=TagsSerializer(read_only=True,many=True)
    tags_id = serializers.PrimaryKeyRelatedField(queryset=Tags.objects.all(), write_only=True)
    postedin = PostedSerializer(read_only=True)
    postedin_id = serializers.PrimaryKeyRelatedField(queryset=Posted.objects.all(), write_only=True)

    class Meta:
        model = Blogs

        fields = ('pk','author','title','tags','tags_id','postedin','postedin_id','content','created_at','updated_at')
        read_only_fields=('pk','created_at','updated_at')


    def get_validation_exclusions(self, *args, **kwargs):
        exclusions = super(BlogsSerializer, self).get_validation_exclusions()

        return exclusions + ['author']
    def create(self, validated_data):
        postedin = validated_data.pop('postedin_id')
        tags = validated_data.pop('tags_id')
        blogs = Blogs.objects.create(tags=tags,postedin=postedin, **validated_data)
        return blogs
</code></pre>

<p>Request sent:</p>

<p>{title: "nvnbv", postedin_id: "1", tags_id: ["2", "5", "1", "4"], content: "nmvmvjm"}</p>

<p>response receive:</p>

<p>{tags_id: ["Incorrect type. Expected pk value, received list."]}</p>

<p>I am beginner in Django-rest-framework.How to solve this error.</p>

<p>Thanks in advance ! </p>

<p>thanks @Abdulafaja for your suggestion.</p>

<p>Finally I got solution</p>

<p>BlogsSerializer should be</p>

<pre><code>class BlogsSerializer(serializers.ModelSerializer):
    author = AccountSerializer(read_only=True,required=False)
    tags=TagsSerializer(read_only=True,many=True)
    tags_id = serializers.PrimaryKeyRelatedField(queryset=Tags.objects.all(), write_only=True,many=True)
    postedin = PostedSerializer(read_only=True)
    postedin_id = serializers.PrimaryKeyRelatedField(queryset=Posted.objects.all(), write_only=True)

    class Meta:
        model = Blogs

        fields = ('pk','author','title','tags','tags_id','postedin','postedin_id','content','created_at','updated_at')
        read_only_fields=('pk','created_at','updated_at')


    def get_validation_exclusions(self, *args, **kwargs):
        exclusions = super(BlogsSerializer, self).get_validation_exclusions()

        return exclusions + ['author']
    def create(self, validated_data):
        postedin = validated_data.pop('postedin_id')
        tags = validated_data.pop('tags_id')
        blogs = Blogs.objects.create(postedin=postedin, **validated_data)
        for tg in tags:
            blogs.tags.add(tg)
        return blogs
</code></pre>

python django django-rest-framework