Django novices (including myself) might be curious about whether a field in a Django model can be set with a manual arithmetic or logical formula. If we think of tables in Microsoft Excel, this is certainly possible and intuitive.
For instance,
class Employee(models.Model):
...
...
...
name = ''.join(
[lastname.value_to_string(),
',',
firstname.value_to_string(),
'']
)
class Meta:
ordering = ['lastname', 'firstname']
This, however, is not what should be done with a Django field. Instead, declare a function for this purpose, and mark it with the @property decorator to make it a normal attribute.
@property
def name(self):
return ''.join([self.lastname, ',', self.firstname, ' '])
Essentially, the @property decorator is doing the following, but in a simpler form as a decorator:
def _get_name(self):
return ''.join([self.lastname, ',', self.firstname, ' '])
name = property(_get_name)