title: Django: POST Pandas dataframe using API, and Pickle or how to "hello world" serializer and view for binary data within Django REST Framework
body: <p>This is a question building upon <a href="https://stackoverflow.com/questions/51473693/how-to-send-a-pandas-dataframe-using-post-method-and-receive-it-in-hug-other-res">How to send a pandas dataframe using POST method and receive it in Hug/other REST API...</a></p>
  
  <ul>
  <li>How do I send binary data (the pickled Pandas Dataframe) using Django REST API to a models.BinaryField?</li>
  </ul>
  
  <p>Below are my tries, but cannot get it to work.</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Want to send the pickled_b64 object, by POST via API to destination (www.foreignserver.com)</p>
  
  <pre><code>import requests
  r = requests.post('http://www.foreignserver.com/api/DataFrame/', data = {'df_object':pickled_b64})
  </code></pre>
  
  <p>.
  .</p>
  
  <h1>On Server www.foreignserver.com</h1>
  
  <p><strong>using <em>for Django REST Framework v. 3.9.4</em></strong></p>
  
  <p><strong><em>models.py</em></strong></p>
  
  <pre><code>class DataFrame(models.Model):
      df_object = models.BinaryField(blank=True, null=True)
  </code></pre>
  
  <p><strong><em>serializers.py</em></strong></p>
  
  <pre><code>class DataFrameSerializer(serializers.HyperlinkedModelSerializer):
      class Meta:
          model = DataFrame
          fields = ('__all__')
  </code></pre>
  
  <p><strong><em>views.py</em></strong></p>
  
  <pre><code>class DataFrameView(viewsets.ModelViewSet):
      queryset = DataFrame.objects.all()
      serializer_class = DataFrameSerializer
  </code></pre>
  
  <h1><strong>Result:</strong></h1>
  
  <ul>
  <li><p>==> 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.</p>
  
  <p>[09/Sep/2019 13:23:53] "POST /api/DataFrame/ HTTP/1.1" 201 64</p></li>
  </ul>
  
  <p>To check if it memoryview address actually is empty, I retrieved the presumed object and analysed it with:</p>
  
  <pre><code>print(list(object))
  len(object)
  </code></pre>
  
  <p>both turned out as 0.</p>
  
  <p><strong>After some digging around I found that:</strong></p>
  
  <blockquote>
    <p>"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: <a href="https://stackoverflow.com/questions/45816305/django-rest-do-not-show-postgresql-binary-field-in-view">Django Rest do not show postgresql binary field in view</a></p>
  </blockquote>