Python Forum
Is it possible to perform a PUT request by passing a req body instead of an ID - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Is it possible to perform a PUT request by passing a req body instead of an ID (/thread-16248.html)



Is it possible to perform a PUT request by passing a req body instead of an ID - ary - Feb-20-2019

I'm working on a Project using Python(3), Django(1.11) and DRF(3.6) in which I have to perform a PUT request by passing an Object instead of an ID.

Here are my models.py:
class Actor(models.Model):
    id = models.CharField(primary_key=True, max_length=255)
    login = models.CharField(max_length=255)
    avatar_url = models.URLField(max_length=500)


class Repo(models.Model):
    id = models.CharField(primary_key=True, max_length=255)
    name = models.CharField(max_length=255)
    url = models.URLField(max_length=500)


class Event(models.Model):
    id = models.CharField(primary_key=True, max_length=255)
    type = models.CharField(max_length=255)
    actor = models.ForeignKey(Actor, related_name='actor')
    repo = models.ForeignKey(Repo, related_name='repo')
    created_at = models.DateTimeField()
And here are my serializers.py:
class ActorSerializer(serializers.ModelSerializer):
    class Meta:
        model = Actor
        fields = ('id', 'login', 'avatar_url')
        read_only_fields = ('id', 'login')

    def update(self, instance, validated_data):
        instance.avata_url = validated_data.get('avatar_url', instance.avatar_url)
        return instance


class RepoSerializer(serializers.ModelSerializer):
    class Meta:
        model = Repo
        fields = ('id', 'name', 'url')


class EventModelSerializer(serializers.ModelSerializer):
    actor = ActorSerializer(many=False)
    repo = RepoSerializer(many=False)

    class Meta:
        model = Event
        fields = ('id', 'type', 'actor', 'repo', 'created_at')
        depth = 1

    def create(self, validated_data):
        actor = validated_data.pop('actor')
        repo = validated_data.pop('repo')
        actor = Actor.objects.create(**actor)
        repo = Repo.objects.create(**repo)
        return Event.objects.create(actor=actor, repo=repo, **validated_data)
And here's my urls.py:
url(r'^actors/$', views.ActorView.as_view(), name='actors'),
And finally here's the views.py:
class ActorView(generics.GenericAPIView):
    serializer_class = ActorSerializer
    queryset = Event.objects.all()

    def update(self):
        actor = Event.objects.filter(actor_id=self.request.data('id'))
        print(actor)
        return HttpResponse(actor)
When I make a PUT request to the /actors and pass an object in the request body via Postman, it says Method Put not allowed.
how can I make the PUT request by passing a body object?