[Fixed]-Post API using Token from header

1👍

You can set user using context['request'] of form, which is provided by default in GenericAPIViews. http://www.django-rest-framework.org/api-guide/serializers/#including-extra-context

def create(self, validated_data):
    tags_data = validated_data.pop('tag')
    owner = self.context['request'].user
    movie = Movie.objects.create(owner=owner, **validated_data)
    for tag_data in tags_data:
        tag_qs = Tag.objects.filter(name__iexact=tag_data['name'])
        if tag_qs.exists():
            tag = tag_qs.first()
        else:
            tag = Tag.objects.create(**tag_data)
        task.tag.add(tag)
    return movie

Leave a comment