【问题标题】:Update value of a certail column in django post_save signals更新 django post_save 信号中某列的值
【发布时间】:2018-08-08 09:37:10
【问题描述】:

我有一个视频类,其结构如下:

class Video(models.Model):
    video = models.FileField(upload_to="media_items/video", blank=True)
    video_mp4 = models.CharField(blank=True, max_length=500)

    def __unicode__(self):
        return unicode(self.video.name) or u''

signals.py 是这样的

@receiver(post_save, sender = Video)
def insert_video_mp4(sender, instance, created, **kwargs):
    if os.path.exists(created_path):
        #some code
    else:
        #some code to insert value in video_mp4 column

我想按照上面 post_save 信号中的说明更新 video_mp4 的值。 请让我知道如何实现这一目标。 我收到以下错误

RuntimeError at /admin/blog/video/8/change/
maximum recursion depth exceeded while calling a Python object
Request Method: POST
Request URL:    http://localhost:8000/admin/blog/video/8/change/
Django Version: 1.10.5
Exception Type: RuntimeError
Exception Value:    
maximum recursion depth exceeded while calling a Python object
Exception Location: /Users/anaconda2/lib/python2.7/site-packages/django/db/models/fields/__init__.py in __eq__, line 462
Python Executable:  /Users/anaconda2/bin/python
Python Version: 2.7.15
Python Path:    
['/Users/TechnopleSolutions/Desktop/matrix-server-master',
 '/Users/anaconda2/lib/python27.zip',
 '/Users/anaconda2/lib/python2.7',
 '/Users/anaconda2/lib/python2.7/plat-darwin',
 '/Users/anaconda2/lib/python2.7/plat-mac',
 '/Users/anaconda2/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/anaconda2/lib/python2.7/lib-tk',
 '/Users/anaconda2/lib/python2.7/lib-old',
 '/Users/anaconda2/lib/python2.7/lib-dynload',
 '/Users/anaconda2/lib/python2.7/site-packages',
 '/Users/anaconda2/lib/python2.7/site-packages/aeosa']

【问题讨论】:

  • 这里为什么需要信号?你的逻辑是什么?你能解释一下吗?
  • 我正在做一些操作,之后我正在更新列
  • 试试我的答案,更新了
  • 这是一个有趣的问题。由于递归保存调用和 post_save 调用,“堆栈溢出”。
  • @JerinPeterGeorge 感谢好友现在工作。

标签: python django


【解决方案1】:

instance 是对应的 Video 对象。


@receiver(post_save, sender = Video)
def insert_video_mp4(sender, instance, created, **kwargs):
    if os.path.exists(created_path):
        #some code
    else:
        instance.video_mp4="some value"
        instance.save()

更新
上述解决方案将导致maximum recursion depth exceeded while calling a Python object 错误。因此,将模型的逻辑 save() 方法重写为,

class Video(models.Model):
    video = models.FileField(upload_to="media_items/video", blank=True)
    video_mp4 = models.CharField(blank=True, max_length=500)

    def save(self, *args, **kwargs):
        if os.path.exists(created_path):
        # some code
        else:
            self.video_mp4 = "some text"
        super().save(*args, **kwargs)

    def __unicode__(self):
        return unicode(self.video.name) or u''

注意: 在 post_save 信号中调用 .save() 方法并不是一个好主意

【讨论】:

  • 我已经尝试过了,但出现“异常值:调用 Python 对象时超出最大递归深度”的错误
  • 您可以为您的问题添加完整的回溯吗?
  • @daemon 你有没有覆盖任何东西?
  • 不,不覆盖任何东西
  • 好的。为什么在这里需要信号?你的逻辑是什么?你能解释一下吗?
【解决方案2】:

感谢@Jerin Peter George 但是找到了一个很好的解决方案

Check this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 2014-12-18
    • 1970-01-01
    • 2014-05-23
    • 2012-05-31
    • 2012-10-12
    相关资源
    最近更新 更多