0

I was searching through stackoverflow for example of working fileupload APIView (using DRF of latest versions), I've already tried with many different code samples but none worked (some of them are deprecated, some - isn't what i want)

I have these models:

class Attachment(models.Model):
    type = models.CharField(max_length=15, null=False)
    attachment_id = models.CharField(max_length=50, primary_key=True)
    doc = models.FileField(upload_to="docs/", blank=True)

I don't wanna use forms and anything else but rest parsers I want to get POST'ed fields (for example name) in future

I believe the solution is easy but this doesnt work

class FileUploadView(APIView):
    parser_classes = (FileUploadParser,)

    def post(self, request):
        file_obj = request.FILES
        doc = Attachment.objects.create(type="doc", attachment_id=time.time())
        doc.doc = file_obj
        doc.save()
        return Response({'file_id': doc.attachment_id}, status=204)
Chickenfresh
  • 365
  • 5
  • 15
  • Doesnt work is not quite descriptive enough ... – Joran Beasley Jul 09 '18 at 23:36
  • @JoranBeasley in current example it says `AttributeError at /file_upload/ 'dict' object has no attribute '_committed'` – Chickenfresh Jul 09 '18 at 23:43
  • im posting file with header Content-Disposition: file; filename="test", the problem appears after .save() - I simply cant save file – Chickenfresh Jul 09 '18 at 23:45
  • files is a dict ... perhaps you want `request.FILES['my_file']` – Joran Beasley Jul 09 '18 at 23:51
  • @JoranBeasley okay, then it writes with no file extension or broken – Chickenfresh Jul 09 '18 at 23:56
  • here is a blog post from a few months ago that looks relevant ... https://blog.vivekshukla.xyz/uploading-file-using-api-django-rest-framework/ ... (and totally different than what you are doing) – Joran Beasley Jul 10 '18 at 00:01
  • @JoranBeasley I've tried this, It doesn't create any file (and returns empty list) :( – Chickenfresh Jul 10 '18 at 00:16
  • keep trying ... im sure it works and im sure youll get there, but you wont be able to get much more help than what you have been given in various google searches ... and its hard with django to provide a [mcve] ... – Joran Beasley Jul 10 '18 at 00:19
  • Is there a good reason to handle this manually? It seems like you could set `doc` as the default "type" and `time.time()` as the default "attachment_id" and let DRF ViewSets do their thing? – MrName Jul 10 '18 at 00:40
  • @MrName 'doc' and `time.time()` are more like an example, I'll manage it my way but what is DRF ViewSets solution? If I could upload file anyhow (safely, without being damaged) then I could simply change Model fields using returned id – Chickenfresh Jul 10 '18 at 00:48
  • I've also tried to implement solution from May 24 (https://stackoverflow.com/questions/20473572/django-rest-framework-file-upload) but it seems incompleted – Chickenfresh Jul 10 '18 at 00:51

1 Answers1

2

removing parser_class will solve almost all problems here. Try the following snippet

class FileUploadView(APIView):

    def post(self, request):
        file = request.FILES['filename']
        attachment = Attachment.objects.create(type="doc", attachment_id=time.time(), doc=file)
        return Response({'file_id': attachment.attachment_id}, status=204)


Screenshot of POSTMAN console
enter image description here

JPG
  • 82,442
  • 19
  • 127
  • 206