Django, REST: serialize a text or image file for publishing via HTTP in JSON

Launch : Windows 7, Python 3.3. Django 1.6

Background . I created an application in Django using a REST framework that accepts HTTP POST requests with descriptions of JSON objects in the body and creates entries in the SQL database from those POST 'ones. Most of the fields in such a database record are integers, such as numOfTests or char fields, such as result . These fields are deserialized in Django by the REST frameworks serializers.py file, which (as far as I know) translates the empty json string into an object in Django, which then saves them to the SQL database with the appropriate commands. But in each table entry, we also need to save a text file, as well as a screenshot, and I did not understand how to send them via HTTP and JSON.

Now I read how to upload an image file through the Django shell in the previous question . But all data records to the database will be performed by remote computers in my case, all through json in the body of HTTP requests.

Question:. Is there a way to serialize the image file or txt file, so various remote computers in our office can send them via json to the django server?

Note These HTTP POST requests will be sent by scripts, in our case, every time the test finishes (python), it sends an HTTP POST to our server URL using the completion details. Thus, a person does not need to manually enter a shell to save the image in Django.

+1
json python rest django serialization
source share
2 answers

Yes, you can encode images using base64 and just place them in the request, but it's a bit of a hack. If you just save base64 in the database, then you will get a huge database, which will be bad.

Here's a snippet that uses base64 on the wire, but saves it as an ImageField:

http://www.snip2code.com/Snippet/93486/Django-rest-framework---Base64-image-fie

The only problem I see with is that debugging using Firebug (or similar) is much more difficult, as additional data is loaded on the explorer.

+3
source

I think this is the cleanest and shortest way to do this.

Let's say you have a model as follows:

 Class MyImageModel(models.Model): image = models.ImageField(upload_to = 'geo_entity_pic') data=model.CharField() 

Thus, the corresponding serializer will look like this:

  from drf_extra_fields.fields import Base64ImageField Class MyImageModelSerializer(serializers.ModelSerializers): image=Base64ImageField() class meta: model=MyImageModel fields= ('data','image') def create(self, validated_data): image=validated_data.pop('image') data=validated_data.pop('data') return MyImageModel.objects.create(data=data,image=image) 

The corresponding view may be as follows:

 elif request.method == 'POST': serializer = MyImageModelSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=201) return Response(serializer.errors, status=400) 

Note. In the Serializer, I used the Base64ImageField implementation provided in the django-extra-field module

To install this module, run the command

 pip install pip install django-extra-fields 

Import the same and do it!

Send (via the post method) your image as a Base64 encoded String in a JSON object along with any other data that you have.

0
source

All Articles