1

I am trying to save the value of a field in an instance variable on my Video model and then use this instance variable in a post_save signal receiver to see if the value of the field has changed.

I am going off of this Stack Overflow answer:

https://stackoverflow.com/a/36722021/

Here is my Video model:

class Video(models.Model):
    approval = models.BooleanField(default=True)

    def __init__(self, *args, **kwargs):
        super(Video, self).__init__(*args, **kwargs)
        self.__original_approval = self.approval

Here is my post_save signal receiver:

@receiver(post_save, sender=Video)
def handle_video_save(sender, **kwargs):
    video = kwargs.get('instance')
    previous_approval = video.__original_approval
    # check if value has changed...

This is the error I'm getting:

AttributeError: 'Video' object has no attribute '__original_approval'

While debugging, I have found that the instance variable can be accessed this way in my post_save signal receiver:

previous_approval = video._Video__original_approval

So, using video._Video__original_approval works, but I'm wondering why am I unable to access the instance variable, as I've seen in other answers on Stack Overflow, with video.__original_approval?

I'm using Django 1.11.20 and Python 3.6.6.

t-payne
  • 3,514
  • 1
  • 6
  • 9
  • I tried using `self.original_approval` without the double underscore and `video.original_approval` and it worked without any problems. So, yes, [this SO question](https://stackoverflow.com/q/1301346) clarifies what I was experiencing using the double underscore and leads me to conclude that [this SO answer](https://stackoverflow.com/a/36722021/) leads to an AttributeError. – t-payne Nov 27 '19 at 17:53

0 Answers0