Error loading a video and creating an object in Django

Asked

Viewed 24 times

1

My function in Django will receive a video from my site, is working normally. So I created the function create:

   def create(self, request):
       Video.objects.create(file=request.data['file'], creator=self.request.user)
       return Response('ok')

I will set the function my way. But when I upload a file from my disk: request.data['file'] = open('/home/developer/Pictures/teste.webm') You’re giving me a terrible mistake:

   def create(self, request):
       request.data['file'] = open('/home/developer/Pictures/teste.webm')
       Video.objects.create(file=request.data['file'], creator=self.request.user)
       return Response('ok')

Output: *** TypeError: readonly attribute

So I checked the type of my variables.

(Pdb) type(request.data['file'])
<class 'django.core.files.uploadedfile.InMemoryUploadedFile'>
(Pdb) request.data['file'] = open('/home/developer/Pictures/teste.webm')
(Pdb) type(request.data['file'])
<type 'file'>

Ok, I know my variables have different types, but how I make my local file in type: <class 'django.core.files.uploadedfile.InMemoryUploadedFile'>

  • Have you tried File(file=open('...')) where from django.core.files.base import File? In fact, the mistake you made there is because you are trying to assign a value in requests.data, which is read-only.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.