[Solved]-Django model instance variables for transient use


That’s an acceptable way of doing it, although you don’t need to initialize it unless there’s a chance you may try to access it when it doesn’t exist. Also, consider if you should be using a property instead.


Update for Python 3: You can call super() directly without repeating the class name

class MyModel (models.Model):
    name = models.CharField(max_length=300)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.tempvar = ''

Leave a comment