简体   繁体   English

如何使用models.FileField将文件保存在数据库中而不是上传到服务器媒体文件夹中?

[英]How to Save file in database instead of upload in server media folder ,using models.FileField?

I want to save a file in database instead of uploading it to a server media file and save URL in Database, I want directly save file in the Database. 我想将文件保存在数据库中,而不是将其上传到服务器媒体文件中,然后将URL保存在数据库中,我想直接将文件保存在数据库中。

Right now I am doing this: 现在我正在这样做:

model.py: model.py:

File = models.FileField(upload_to='CSR/', null=True , blank = True)

template.html: template.html:

<td> {{certificate.File.url}} </td>

Django supports BinaryField which lets you store binary data in the database. Django支持BinaryField ,可让您将二进制数据存储在数据库中。

The Django docs state: Django docs状态:

Abusing BinaryField 滥用BinaryField

Although you might think about storing files in the database, consider that it is bad design in 99% of the cases. 尽管您可能考虑过将文件存储在数据库中,但在99%的情况下,请考虑这样做是不好的设计。 This field is not a replacement for proper static files handling. 该字段不能代替适当的静态文件处理。

If you do not want to store files into the filesystem of your webserver, you can use the django-storages library which provides options for Amazon S3, DigitalOcean and more. 如果您不想将文件存储到Web服务器的文件系统中,则可以使用django-storages库,该库为Amazon S3,DigitalOcean等提供选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM