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

时间:2013-02-19 16:41:24

标签: django django-models

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

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

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

由于

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)

4 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:2)

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

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)

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

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

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