更新Django post_save信号中certail列的值

时间:2018-08-08 09:37:10

标签: python django

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

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

我想要更新video_mp4的值,如上文在post_save信号中所述。 请让我知道如何实现这一目标。 我收到以下错误

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']

2 个答案:

答案 0 :(得分:1)

SELECT address_id, company_name, invoice_batch_billing, frequency_type_id, date_of_invoice_id FROM pls.address where invoice_style='Consolidated' and ((frequency_type_id=1 and date_of_invoice_id=2) or (frequency_type_id=2 and date_of_invoice_id=2) or (frequency_type_id=3 and date_of_invoice_id=2)) 在此处是对应的 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''信号内调用 .save() 方法不是一个好主意

答案 1 :(得分:0)

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

Check this