[英]How to sync photos automatically with database in Django?
我是Python和Django的新手。
我正在一个需要Django的项目中。 在此项目中,程序会通过相机连续捕获照片,并将其存储在路径C:\\Users\\admin\\Pictures
。
现在,我希望Django APP自动将这些照片与数据库同步,而不是登录Django管理站点并手动上传。 如何在Django中正确执行此操作?
这是models.py
from __future__ import unicode_literals
from django.db import models
from django.conf import settings
from django.utils.encoding import python_2_unicode_compatible
# Create your models here.
@python_2_unicode_compatible
class Image(models.Model):
title = models.CharField(max_length=250, blank=True)
original = models.ImageField(upload_to=settings.IMAGE_PREFIX, default='/tmp/none.jpg')
def __str__(self):
return self.title
我写脚本只是想同步这些照片:
import os
import django
django.setup()
from gallery.models import Image
path = r"C:\Users\admin\Pictures"
for filename in os.listdir(path):
filePath = os.path.join(path, filename)
thisFile = open(filePath, 'rb')
new_img = Image(original=thisFile, title=filename)
new_img.save()
但是我收到如下错误消息:
我thisFile
我尝试将thisFile
传递给Image()
时肯定有一些遗漏,但是我只是不知道如何解决。
非常感谢 !
FileField
需要Django File
对象。
import os
import django
from django.core.files import File
django.setup()
from gallery.models import Image
path = r"C:\Users\admin\Pictures"
for filename in os.listdir(path):
filePath = os.path.join(path, filename)
thisFile = File(open(filePath, 'rb'))
new_img = Image(original=thisFile, title=filename)
new_img.save()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.