【发布时间】:2017-06-21 21:32:50
【问题描述】:
我正在尝试根据模型生成二维码。我在 Heroku 托管应用程序并使用 AWS S3 作为存储。 S3 的存储与其他模型元素完全兼容,只是用于生成 QR 码的模型有问题。我使用这个链接作为参考: https://gilang.chandrasa.com/blog/generate-qr-code-in-django-model/ 我的模型是:
class BusinessQRCode(models.Model):
business = models.ForeignKey(Business, null=True)
location_name = models.CharField(max_length=255)
qrcode = models.ImageField(upload_to='documents/{}'.format(time.strftime("%Y/%m/%d")), blank=True, null=True)
def save(self):
super(BusinessQRCode, self).save()
self.generate_qrcode()
def generate_qrcode(self):
from activation.models import RandomFileName
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data('Some data')
qr.make(fit=True)
filename = 'qrcode-%s.png' % self.id
img = qr.make_image()
from django.conf import settings
img.save(settings.MEDIA_ROOT + filename)
# reopen = open(settings.MEDIA_ROOT + filename, "rb")
# django_file = File(reopen)
self.qrcode.save(filename,img, save=True)
上面的代码给了我这个错误:
TypeError
TypeError: seek() takes exactly 2 arguments (3 given)
我也尝试过使用注释代码,即打开文件而不是尝试保存它,但它不起作用,它只是永远不会停止加载。我的意思是最后这部分代码:
reopen = open(settings.MEDIA_ROOT + filename, "rb")
django_file = File(reopen)
self.qrcode.save(filename,django_file, save=True)
我做错了什么?
【问题讨论】:
-
添加错误的回溯。将文件保存到 S3 后端时,您应该使用 django
File对象,如上一个示例所示。img是什么类型? S3 后端不会直接从您的本地驱动器读取文件。 -
@HåkenLid img 属于 PIL Pillows 图像类型。我将放置堆栈跟踪。
-
ImageField.save 需要一个 django File 对象,所以我猜这就是当你传递 PIL.Image 时它失败的原因。在默认的本地存储中,我认为您可能会通过将路径名作为字符串传递,但 S3 后端更加严格。
标签: python django amazon-s3 django-models django-1.8