Django模型错误超出最大递归深度

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django模型错误超出最大递归深度相关的知识,希望对你有一定的参考价值。

我正在关注这个guide,我在保存时遇到以下错误...

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

为什么我会收到此错误?我已经在下面列出了完整的模型。

谢谢

models.朋友:

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)
答案

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

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)
另一答案

你的帖子保存信号呼叫保存调用保存信号,保存信号...

这基本上是一个无限循环,运行时会因Max Recursion错误而终止。

另一答案

要解决此问题,您应该在数据库中再添加一个字段,该字段将描述通过简单视图或信号插入插入的记录。然后在接收器功能中你应该检查是否通过信号插入实例,如果这样不应用接收器功能

@receiver(post_save) def my_callback(using,sender,instance,**kwargs):
   if not(instance.added_by == 'signal'):
       obj = Student1()
       obj.name = instance.name
       obj.city = instance.city
       obj.state = instance.state
       obj.postal_code = instance.postal_code
       obj.age = instance.age
       obj.added_by = 'signal'
       obj.save()
另一答案

我的方法只是使用QuerySet对象的udpate方法而不是从Model中保存一个:

Model.objects.filter(pk=obj.pk).update(fieldx=valuey)

以上是关于Django模型错误超出最大递归深度的主要内容,如果未能解决你的问题,请参考以下文章

Django url标签:超出最大递归深度

Python递归函数错误:“超出最大递归深度” [重复]

Celery 4.2-Django RecursionError:超出最大递归深度

Python:超出最大递归深度

从 n = 8 开始的错误:“调用 Python 对象时超出了最大递归深度”[重复]

RecursionError:超出最大递归深度