Is there any way to do in django custom property setter like this?
class MyModel(models.Model):
myfield = models.CharField(length = 250)
@myfield.setter
def set_password(self, value):
self.password = encrypt(value)
Is there any way to do in django custom property setter like this?
class MyModel(models.Model):
myfield = models.CharField(length = 250)
@myfield.setter
def set_password(self, value):
self.password = encrypt(value)
You will really set the value on saving the model, so it's better to override save()
method (ot use pre_save
signal).
What's wrong with a method?
instance.set_password('my_pw')
You can use @property
to define setters:
http://docs.python.org/library/functions.html#property
### Pasted from docs
class C(object):
def __init__(self):
self._x = None
@property
def x(self):
"""I'm the 'x' property."""
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x