#30735: Testing client encode_multipart may also support dict format ---------------------------------------------+------------------------ Reporter: ychab | Owner: nobody Type: New feature | Status: new Component: Testing framework | Version: 2.2 Severity: Normal | Keywords: test Triage Stage: Unreviewed | Has patch: 1 Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 1 UI/UX: 0 | ---------------------------------------------+------------------------ When using implicitly multipart-form data with the testing client (post / put methods), we can't unfortunetly provide data like dict, whereas we could provide file, list and even list of files... Of course, the actual workaround is to do it manually (cast dict to json) but it should be really great if it could be done automatically!
Furthermore, some third party libraries like Django Rest Framework are limited by this, see https://github.com/encode/django-rest- framework/blob/3.10.2/rest_framework/renderers.py#L907 Concretly with DRF, we would be able to '''test''' file upload '''with''' nested data on an endpoint at the same time. I highlight ''test'' because otherwise in real life, it works if the HTTP client send the dict data as a json string. Here is just a basic example, with the native Django test client where we '''can't''' do this: {{{ with open(filepath, 'rb') as fp: response = self.client.post('/post/', data={ 'nested_data': { 'firstname': 'foo', 'lastname': 'foo', }, 'testfile': fp, }) }}} Indeed instead, we have to cast it manually: {{{ import json with open(filepath, 'rb') as fp: response = self.client.post('/post/', data={ 'nested_data': json.dumps({ 'firstname': 'foo', 'lastname': 'foo', }), 'testfile': fp, }) }}} In that case, it would be ok and nested data would be properly parsed. Finally, the feature request should not be so hard and I have already a working patch. However, I need first to know if it is an acceptable feature request? Am I missing something else? Thanks for the review -- Ticket URL: <https://code.djangoproject.com/ticket/30735> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/048.60c017f0a590354853270aaa1fd2be09%40djangoproject.com.