I would like to know what would be the best practice. I have been using the @property
decorator a lot because it allows me to avoid creating custom context variables when I want to display something related to a model instance on a template.
I feel like my models are too big. Is this ok?
The @property is a built-in decorator for the property() function in Python. It is used to give "special" functionality to certain methods to make them act as getters, setters, or deleters when we define properties in a class.
A python @property decorator lets a method to be accessed as an attribute instead of as a method with a '()' . Today, you will gain an understanding of when it is really needed, in what situations you can use it and how to actually use it.
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.
@property decorator is a built-in decorator in Python which is helpful in defining the properties effortlessly without manually calling the inbuilt function property().
It's not a problem unless you find yourself writing exactly the same code on model after model. At that point you should consider writing a template tag that takes the model as a parameter instead.
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