ArtOfWarfare
ArtOfWarfare

Reputation: 21476

Django Rest Framework says No input provided?

I want to make it so that I can upload files and text to my server endpoint.

Here's an example post from curl that I want to have work:

curl -X POST https://mydomain.com/myapp/ -u user:pass -F "body=test" -F "eyecon.png=@/Users/myaccount/myproject/eyecon.png"

The problem is, when I run this, I get this error message:

{"non_field_errors": ["No input provided"]}

I looked through the Django Rest Framework code and it only generates this error message from a single location, which is for handling the case when request.DATA and request.FILES are both empty/missing. So I suspect that my issue is somewhere in a configuration file (could be Apache, Django, or Django Rest Kit's configuration that I messed up). I expect someone will tell me that there's a single line I'm missing somewhere and it'll be a quick and easy fix.

But just incase it isn't a configuration issue and it's actually an issue with my code, here's all of my code related to this:

views.py

class MessageList(generics.ListCreateAPIView):
    permission_classes = (IsAuthenticatedOrReadOnly,)
    queryset           = Message.objects.all()
    serializer_class   = MessageSerializer

    def pre_save(self, obj):
        obj.owner = self.request.user

serializers.py

class MessageSerializer(serializers.ModelSerializer):
    owner = serializers.Field(source='owner.username')

    def __init__(self, instance=None, data=None, files=None,
                 context=None, partial=False, many=None,
                 allow_add_remove=False, **kwargs):
        super(MessageSerializer, self).__init__(**kwargs)
        for attachment in files.getlist('file', []):
            Attachment(target=id, file=attachment).save()

    class Meta:
        model  = Message
        fields = ('id', 'owner', 'title', 'created', 'body')

models.py

class Message(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    title   = models.CharField(max_length=100, blank=True, default='')
    body    = models.TextField()
    owner   = models.ForeignKey('auth.User', related_name='messages')

    class Meta:
        ordering = ('created',)

class Attachment(models.Model):
    target  = models.ForeignKey('server_app.Message', related_name='attachments')
    theFile = models.FileField(upload_to='attachments/')

Upvotes: 1

Views: 640

Answers (1)

ArtOfWarfare
ArtOfWarfare

Reputation: 21476

My issue was in serializers.py. When I call super(...).__init__(**kwargs), I neglected to pass in all the other arguments that were received. Thus the serializer complained about not getting input because I wasn't giving it input.

Corrected it by replacing my prior call to super(...).__init__(...) with:

super(MessageSerializer, self).__init__(self, instance=instance, data=data, files=files,
                                        context=context, partial=partial, many=many,
                                        allow_add_remove=allow_add_remove, **kwargs)

Upvotes: 1

Related Questions