0
votes

Whenever I try a post request, this is the error I get:

TypeError at /api/

Direct assignment to the forward side of a many-to-many set is prohibited. Use project_team.set() instead.

Request Method: POST Request URL: http://127.0.0.1:8000/api/ Django Version: 2.0 Exception Type: TypeError Exception Value:

Direct assignment to the forward side of a many-to-many set is prohibited. Use project_team.set() instead.

Exception Location: C:\Users\Siddhesh\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\related_descriptors.py in set, line 509 Python Executable: C:\Users\Siddhesh\AppData\Local\Programs\Python\Python36\python.exe Python Version: 3.6.3 Python Path:

['C:\Users\Siddhesh\Desktop\TechForSocial\backend', 'C:\Users\Siddhesh\AppData\Local\Programs\Python\Python36\python36.zip', 'C:\Users\Siddhesh\AppData\Local\Programs\Python\Python36\DLLs', 'C:\Users\Siddhesh\AppData\Local\Programs\Python\Python36\lib', 'C:\Users\Siddhesh\AppData\Local\Programs\Python\Python36', 'C:\Users\Siddhesh\AppData\Local\Programs\Python\Python36\lib\site-packages', 'C:\Users\Siddhesh\AppData\Local\Programs\Python\Python36\lib\site-packages\pytz-2018.5-py3.6.egg']

Server time: Mon, 17 Dec 2018 17:57:57

models.py:

class DummyPeopleModel(models.Model):

     person_name = models.CharField(max_length=45)

     def __str__(self):

          return self.person_name


class ActiveProject(models.Model):

     project_name = models.CharField(max_length=30)
     project_abstract = models.CharField(max_length=1000)
     project_paper = models.CharField(max_length=1000)
     project_team = models.ManyToManyField(DummyPeopleModel, help_text='Team that works on this Project' )
     project_join_us = models.CharField(max_length=1000)

     def __str__(self):
         return self.project_name

serializers.py:

from rest_framework import serializers
from .models import ActiveProject


class ActiveProjectSerializer(serializers.ModelSerializer):

     class Meta:

        model = ActiveProject
        fields = ('id', 'project_name', 'project_abstract', 'project_paper', 'project_team', 'project_join_us',)

     def create(self, validated_data):
        return ActiveProject.objects.create(**validated_data)

views.py:

class ProjectList(generics.ListAPIView):
     queryset = ActiveProject.objects.all()
     serializer_class = ActiveProjectSerializer

     def post(self, request):

         serializer = ActiveProjectSerializer(data=request.data)
         if serializer.is_valid():
             serializer.save()
             return Response(serializer.data, status=status.HTTP_201_CREATED)
         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class ProjectDetail(generics.RetrieveUpdateDestroyAPIView):
     queryset = ActiveProject.objects.all()
     serializer_class = ActiveProjectSerializer
1

1 Answers

0
votes

as per the documentation, specifying project_team as a PrimaryKeyRelatedField(many=True) in the serializer might help (don't set it to read_only though)