Django模型错误最大递归深度超出 [英] Django model error maximum recursion depth exceeded

查看:181
本文介绍了Django模型错误最大递归深度超出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注此指南并且在保存时收到以下错误...

  / admin / products / product / 2 / $ b $中的RuntimeError b最大递归深度超过

为什么会收到此错误?我已经包括了完整的模型。



谢谢



models.py:

  class Product(models.Model):
title = models.CharField(max_length = 60)
qr_url = models.URLField(blank =真的)
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 )
#自定义管理器

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
如果instance.qr_image:
instance.qr_image.delete()
qr = QRCode(4,QRErrorCorrectLevel.L)
qr.addData(实例。 qr_url)
qr.make()
image = qr.makeImage()

#将图像保存到字符串缓冲区
image_buffer = StringIO()
图像.save(image_buffer,format ='JPEG')
image_buffer.seek(0)

#我们使用django文件存储系统来保存图像。
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)
pre>

解决方案

您从教程中以错误的方式复制:)

  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()

#将图像保存到字符串缓冲区
image_buffer = StringIO()
image.save(image_buffer,format ='JPEG')
image_buffer。 seek(0)

#我们使用django文件存储系统保存图像。
file_name ='UrlQR_%s.jpg'%instance.id
file_object = File(image_buffer,file_name)
content_file = ContentFile(file_object.read())
instance._already_saving = True
instance.qr_image.save(file_name,content_file,save = True)


I'm following this guide and I get the following error when saving...

RuntimeError at /admin/products/product/2/
maximum recursion depth exceeded

Why do I get this error? I have included the full model below.

Thanks

models.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)

解决方案

You copiend in wrong way from tutorial :)

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._already_saving = True
        instance.qr_image.save(file_name, content_file, save=True)

这篇关于Django模型错误最大递归深度超出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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