1

I have a class Video whose structure is as below:

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

and signals.py is as

@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

I want to update the value of video_mp4 as explained above in post_save signals. Please let me know how to achieve this. I am getting the below error

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']
daemon
  • 113
  • 3
  • 14

2 Answers2

2

instance is corresponding Video object here.


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

UPDATE
The above solution will cause a maximum recursion depth exceeded while calling a Python object error. So, do your logic save() method of model by overriding it as,

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

Note: It's not a good idea to call .save() method inside a post_save signal

JPG
  • 82,442
  • 19
  • 127
  • 206
0

Thanks @Jerin Peter George But found a very good solution for that

Check this

daemon
  • 113
  • 3
  • 14