Django,REST:序列化文本或图像文件以通过HTTP在JSON中发布 [英] Django, REST: Serialize a text or image file to post via HTTP in JSON

查看:167
本文介绍了Django,REST:序列化文本或图像文件以通过HTTP在JSON中发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在运行:Windows 7,Python 3.3。 Django 1.6

Running:Windows 7, Python 3.3. Django 1.6

背景:我在Django中使用REST框架创建了一个应用程序,该框架接受HTTP'POST'请求,并使用JSON描述身体中的对象,并从这些POST请求的SQL数据库中创建记录。这种数据库记录的大多数字段是整数,例如 numOfTests 或像 result 的字符。这些字段通过REST框架 serializers.py 文件反序列化为Django,据我所知,这些文件将简洁的json字符串转换为Django中的对象,然后将其保存到SQL数据库具有相应的命令。但是在每个表格记录中,我们还需要存储一个文本文件以及一个截图,而且我还没有想出如何通过HTTP和JSON发送这些文件。

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.

现在我已经阅读了如何通过Django shell上传图像文件,这里是一个上一个问题。但是,在我的情况下,数据库中的所有数据条目都将由远程计算机完成,全部通过HTTP请求正文中的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.

问题: 那么有没有办法序列化图像文件或txt文件,所以我们办公室的各种远程计算机可以通过json将其发布到django服务器?

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?

注意:这些HTTP POST 请求将通过脚本发送,在我们的例子中,每次测试完成(python),它将发送一个http POST 到我们的服务器URL,其详细信息完成。所以没有人类手动进入shell并将图像保存到Django中。

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.

推荐答案

是的,你可以使用base64对图像进行编码,只是在请求中发布,但这是一个黑客。如果您只是将base64保存到数据库,那么最终会出现一个很糟糕的数据库。

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.

这里有一个片段,在线上使用base64,但会另存为ImageField:

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

我可以看到的唯一问题是使用Firebug(或类似的)更困难,因为线上有额外的数据。

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.

这篇关于Django,REST:序列化文本或图像文件以通过HTTP在JSON中发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆