Django model instance parameters for temporary use

I would like to create some instance variables for my subclass of the model, but when saving the object to the database, I don't need the table column for this variable. I read in some places that you would do this by overriding init (), for example, how to create regular instance variables in other classes. Is this an acceptable way for subclassing a model? Are there other approaches?

models.py:

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

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

views.py:

myModel = MyModel()
myModel.tempvar = 'this will not be saved in the database'
+5
source share
1 answer

, , , . , , .

+3

All Articles