This is a question building upon How to send a pandas dataframe using POST method and receive it in Hug/other REST API...
- How do I send binary data (the pickled Pandas Dataframe) using Django REST API to a models.BinaryField?
Below are my tries, but cannot get it to work.
import pandas as pd
df = pd.DataFrame({'a': [0, 1, 2, 3]})
import pickle
pickled = pickle.dumps(df)
import base64
pickled_b64 = base64.b64encode(pickled)
Want to send the pickled_b64 object, by POST via API to destination (www.foreignserver.com)
import requests
r = requests.post('http://www.foreignserver.com/api/DataFrame/', data = {'df_object':pickled_b64})
. .
On Server www.foreignserver.com
using for Django REST Framework v. 3.9.4
models.py
class DataFrame(models.Model):
df_object = models.BinaryField(blank=True, null=True)
serializers.py
class DataFrameSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = DataFrame
fields = ('__all__')
views.py
class DataFrameView(viewsets.ModelViewSet):
queryset = DataFrame.objects.all()
serializer_class = DataFrameSerializer
Result:
==> A post is created but with Null value, the framework does not save the data package / binary data. However, I am given a 201 response.
[09/Sep/2019 13:23:53] "POST /api/DataFrame/ HTTP/1.1" 201 64
To check if it memoryview address actually is empty, I retrieved the presumed object and analysed it with:
print(list(object))
len(object)
both turned out as 0.
After some digging around I found that:
"BinaryField are not supported by Django REST framework. You'll need to write a serializer field class and declare it in a mapping to make this work." ref: Django Rest do not show postgresql binary field in view