Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Django's @property do?

Tags:

python

django

What is @property in Django?

Here is how I understand it: @property is a decorator for methods in a class that gets the value in the method.

But, as I understand it, I can just call the method like normal and it will get it. So I am not sure what exactly it does.

Example from the docs:

from django.db import models  class Person(models.Model):     first_name = models.CharField(max_length=50)     last_name = models.CharField(max_length=50)     birth_date = models.DateField()      def baby_boomer_status(self):         "Returns the person's baby-boomer status."         import datetime         if self.birth_date < datetime.date(1945, 8, 1):             return "Pre-boomer"         elif self.birth_date < datetime.date(1965, 1, 1):             return "Baby boomer"         else:             return "Post-boomer"      @property     def full_name(self):         "Returns the person's full name."         return '%s %s' % (self.first_name, self.last_name) 

What is the difference of if it is there vs if it isn't?

like image 775
Jordan Avatar asked Oct 25 '19 12:10

Jordan


People also ask

What is the use of @property in Python?

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.

What is Property decorator in Python?

The @property Decorator In Python, property() is a built-in function that creates and returns a property object. The syntax of this function is: property(fget=None, fset=None, fdel=None, doc=None) where, fget is function to get value of the attribute. fset is function to set value of the attribute.

What is the use of class meta in Django?

Model Meta is basically the inner class of your model class. Model Meta is basically used to change the behavior of your model fields like changing order options,verbose_name, and a lot of other options. It's completely optional to add a Meta class to your model.

What are models in Django used for?

Django web applications access and manage data through Python objects referred to as models. Models define the structure of stored data, including the field types and possibly also their maximum size, default values, selection list options, help text for documentation, label text for forms, etc.


1 Answers

As you see, the function full_name returns a string with the persons first and last name.

What the @property decorator does, is declare that it can be accessed like it's a regular property.

This means you can call full_name as if it were a member variable instead of a function, so like this:

name = person.full_name

instead of

name = person.full_name()

You could also define a setter method like this:

@full_name.setter def full_name(self, value):      names = value.split(' ')      self.first_name = names[0]      self.last_name = names[1] 

Using this method, you can set a persons full name like this:

person.full_name = 'John Doe' 

instead of

person.set_full_name('John Doe') 

P.S. the setter above is just an example, as it only works for names that consist of two words separated by a whitespace. In real life, you'd use a more robust function.

like image 89
Nico Griffioen Avatar answered Oct 04 '22 02:10

Nico Griffioen