Why doesn't django.test.client call _encode_data for a put request?

65 views
Skip to first unread message

SKisContent

unread,
Oct 18, 2018, 8:54:12 AM10/18/18
to Django users
Hi.

The Django Rest Framework encourages the use of put for uploading files. However, django.test.client.Client does not encode multipart data before simulating the server call. Does anyone know why this has been left out?

The code in django.test.client for put() is:

def put(self, path, data='', content_type='application/octet-stream', secure=False, **extra):
 """Construct a PUT request."""
 data = self._encode_json(data, content_type)
 return self.generic('PUT', path, data, content_type, secure=secure, **extra)

The code in for post() is:

def post(self, path, data=None, content_type=MULTIPART_CONTENT, secure=False, **extra):
 
"""Construct a POST request."""
 data
= self._encode_json({} if data is None else data, content_type)
 post_data
= self._encode_data(data, content_type)

 
return self.generic('POST', path, post_data, content_type, secure=secure, **extra)

As you can see the put method doesn't call _encode_data. So if the data happens to include a file and I'm trying to test a file upload, it doesn't work.

The last issue that I found related to this question appeared in the Django issues archive in 2009-10. 

Thanks!

Jason

unread,
Oct 19, 2018, 11:10:52 AM10/19/18
to Django users
don't conflate django rest framework with the django project.  despite the name, they're two separate projects.

I would use DRF's test clients instead of django's.
Reply all
Reply to author
Forward
0 new messages