I am new to Django and am trying to create an application to check out a few things for my project. I want to read the form - do some validation, and then submit it to another module (say, the scheduler works separately). The rest api scheduler will be called with the form data (which is the file), and the scheduler will load the data in the model. I am using python requests and serializing data in json before calling rest api. Here I get the error. Django upon request. FILES creates an InMemoryUploadedFile class that loads data somewhere in memory, and serializing this in Json is not easy. I tried looking for other ways (for example, an example of an image serializer), but could not solve this problem.
forms.py
class UploadDatasetForm(forms.Form): docfile = forms.FileField(label='Choose file')
views.py
def test_upload(request): if request.method == 'POST': form = UploadDatasetForm(request.POST, request.FILES) if form.is_valid(): in_file = request.FILES['docfile'] payload = {'doc_file': in_file} msg = json.dumps(payload) URL = 'http://localhost:8880/form' r = requests.post(URL, data=msg) return HttpResponse(json.dumps(r.text), content_type="application/json")
Error:
raise TypeError(repr(o) + " is not JSON serializable") TypeError: <InMemoryUploadedFile: A_test.csv (text/csv)> is not JSON serializable
Any help here would be appreciated. Thank you very much.
source share