在Django中临时保存和清理图像对象 [英] Tamporarily saving and sanitizing image objects in Django

查看:46
本文介绍了在Django中临时保存和清理图像对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Django网站,用户可以在其中发布要出售/易货的二手物品的详细信息.

I'm creating a Django website where users post details of used items they want to sell/barter.

发布商品时,倒数第二步是上传(最多3张)正在出售的商品的照片.最后一步是提供个人详细信息(姓名,地址,手机).此后,广告将最终确定并进入待批准"队列.

When posting an item, the second-last step is to upload (upto 3) photos of the item on sale. The last step is to provide personal details (name, address, mobile). After this, the ad is finalized and goes into a "pending approval" queue.

在广告定案之前,我不想将照片保存到数据库中..因此,我认为我将暂时保存对从表单中检索的UploadedFile 对象如下:

I don't want to save photos to the DB until an advert is finalized. Hence I'm thinking I'll temporarily save a reference to the UploadedFile object retrieved from the form like so:

request.session["photo"] = form.cleaned_data.get('photo',None)

到目前为止,一切都很好,还是有问题?假设到目前为止一切都正确.

So far so good, or is this problematic? Assuming everything is correct so far.

接下来,一旦广告结束,我将图像保存到我的存储后端,并从 request.session 字典中 pop 照片

Next, once ad is final, I'll save the images to my storage backend and pop photo from the request.session dictionary.

但是,如果用户在完成广告之前退出了该怎么办?如何处理创建 request.session ["photo"] 条目的情况,但用户从未完成广告?UploadedFile对象保存在哪里?

But what if the user drops out before finalizing the ad? How would I handle cases where a request.session["photo"] entry was created, but the user never finished the ad? Where are UploadedFile objects saved?

基本上,我需要一种有效的方法来处理孤立的" request.session图像条目和相关的UploadedFile对象.

Basically, I need an efficient way to process 'orphaned' request.session image entries and related UploadedFile objects.

推荐答案

使用会话的主要问题是它们无意处理大量数据,并且由于编码,RAM的存在,它们可能会丢失信息.填充,框架的垃圾收集过程等.这实际上取决于您存储会话对象的方式,如果您已经解决了此问题,请转到第二步.

The main problem of using sessions is that they're not meant to handle a lot amount of data, and they could potentially loss information due to encoding, the RAM filling up, the framework's process of garbage collection, and such. That really dependes on how you store sessions objects, if you already solved this problem, go to step two.

第一步:您可能要创建一个单独的模型来临时存储图像,并通过日期字段(通过ImageField)存储日期和图像文件参考,您可以告诉该模型将文件上传到使用字段的 upload_to 参数在Media目录上的临时位置.

Step One: You may want to make a separate model to store the image temporarily, storing the date and image file reference (through an ImageField), you can tell this model to upload the file to a temporary location on the Media directory using the upload_to parameter of the field.

第二步:设置管理命令,该命令从特定日期(图像早于10分钟左右)过滤掉孤立的模型,然后将其与图像一起删除(这非常重要),因为删除数据库记录不会删除文件).管理命令应该能够在没有用户干预的情况下运行.在此处详细了解管理命令.

Step Two: Set up a management command that filters the orphaned models from a certain date (images older than 10 minutes or so), and then deletes them along with the images (this is very important, as deleting the database record does not delete the file). The management command should be able to run without user intervention. Read more about management commands here.

第三步:设置计算机的Crontab或用于计划进程的任何内容,以根据需要每天或每小时运行管理命令.

Step Three: Set up your machine's Crontab or whatever you use to schedule processes, to run the management command in a daily or hourly basis depending on your needs.

这篇关于在Django中临时保存和清理图像对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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