Django Fields. Custom Field Value Installer

Is there any way to do this in setting up a custom django property?

class MyModel(models.Model): myfield = models.CharField(length = 250) @myfield.setter def set_password(self, value): self.password = encrypt(value) 
+6
source share
2 answers

You really set the value to save the model, so it's better to override the save() method (use the pre_save signal).

+1
source

What happened to the 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 
+1
source

Source: https://habr.com/ru/post/900180/


All Articles