Django, REST: Serialize a text or image file to po

2019-07-24 02:43发布

Running:Windows 7, Python 3.3. Django 1.6

Background: I'm created an app in Django using the REST framework, that accepts HTTP 'POST' requests with JSON descriptions of objects in the body and creates records in a SQL databse from those 'POST' requests. Most of the fields of such a database record are either integers, like numOfTests or char fields like result. These fields are deserialized into Django by the REST frameworks serializers.py file, which (to my knowledge) translate the plain json string into an object in Django, who then saves them into the SQL database with the appropriate commands. But in each table record we also need to store a text file, as well as a screenshot, and I haven't figured out how to send these via HTTP and JSON.

Now I've read how to upload a image file via the Django shell, here in a previous question. But all of the data entries into the databse will be done by remote computers in my case, all via json in the body of the HTTP requests.

Question: So is there a way to serialize an image file or txt file, so the various remote computers at our office can post them via json to the django server?

Note: These HTTP POST requests will be sent by scripts, in our case specifically every time a test finishes (python) it will send an http POST to our server url with the details about it's completion. So there's no human being to manually enter the shell and such to save an image into Django.

2条回答
等我变得足够好
2楼-- · 2019-07-24 03:14

Yeah you could encode the images using base64 and just post them in the request but it's a bit of a hack. If you just save the base64 to the database then you will end up with a huge database which is bad.

There is a snippet here that uses base64 on the wire but saves as an ImageField:

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

The only problem I can see with this is it makes debugging using Firebug (or similar) much more difficult as there is loads of extra data on the wire.

查看更多
一夜七次
3楼-- · 2019-07-24 03:14

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

Let say you have a Model as follows:

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

So the Corresponding Serializer would be as follows:

 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 can 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)

Notice In the Serializer I have used Implementation of Base64ImageField provided in the module django-extra-field

To install this module run the command

pip install pip install django-extra-fields

Import the same and Done!

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

查看更多
登录 后发表回答