我正在按照本指南进行操作,但在保存时出现以下错误...
RuntimeError at /admin/products/product/2/
maximum recursion depth exceeded
为什么我会收到此错误?我在下面包含了完整的模型。
谢谢
模型.py:
class Product(models.Model):
title = models.CharField(max_length=60)
qr_url = models.URLField(blank=True)
qr_image = models.ImageField(
upload_to="public/uploads/",
height_field="qr_image_height",
width_field="qr_image_width",
null=True,
blank=True,
editable=False
)
qr_image_height = models.PositiveIntegerField(null=True, blank=True, editable=False)
qr_image_width = models.PositiveIntegerField(null=True, blank=True, editable=False)
#FK
category = models.ManyToManyField(ProductCategory)
attribute_answers = models.ManyToManyField(AttributeAnswers)
# Custom Managers
def __unicode__(self):
return self.title
def qr_code(self):
return '' % self.qr_image.url
qr_code.allow_tags = True
def product_pre_save(sender, instance, **kwargs):
if not instance.pk:
instance._QRCODE = True
else:
if hasattr(instance, '_QRCODE'):
instance._QRCODE = False
else:
instance._QRCODE = True
models.signals.pre_save.connect(product_pre_save, sender=Product)
def product_post_save(sender, instance, **kwargs):
if instance._QRCODE:
instance._QRCODE = False
if instance.qr_image:
instance.qr_image.delete()
qr = QRCode(4, QRErrorCorrectLevel.L)
qr.addData(instance.qr_url)
qr.make()
image = qr.makeImage()
#Save image to string buffer
image_buffer = StringIO()
image.save(image_buffer, format='JPEG')
image_buffer.seek(0)
#Here we use django file storage system to save the image.
file_name = 'UrlQR_%s.jpg' % instance.id
file_object = File(image_buffer, file_name)
content_file = ContentFile(file_object.read())
instance.qr_image.save(file_name, content_file, save=True)
models.signals.post_save.connect(product_post_save, sender=Product)