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)
BigAutoField is a 64-bit integer, much like an AutoField except that it is guaranteed to fit numbers from 1 to 9223372036854775807. One can create a BigAutoField using the following syntax, id = models.BigAutoField(primary_key=True, **options) This is an auto-incrementing primary key just like AutoField.
max_length. Required. The maximum length (in characters) of the field. The max_length is enforced at the database level and in Django's validation using MaxLengthValidator .
The on_delete method is used to tell Django what to do with model instances that depend on the model instance you delete. (e.g. a ForeignKey relationship). The on_delete=models. CASCADE tells Django to cascade the deleting effect i.e. continue deleting the dependent models as well.
Let's look at one example. In Python, @property is a built-in decorator that creates and returns a property object. The @property decorator is internally taking the bounded method as an argument and returns a descriptor object.
You will really set the value on saving the model, so it's better to override save()
method (ot use pre_save
signal).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With