title: Django Model Method or Calculation as Field in Database
body: <p><em>Using Django ~=1.11 and Python 3.6</em></p>
  
  <p>I need to store 'calculated' variables as fields in the Django model database.</p>
  
  <p>Here's a model:</p>
  
  <pre><code>from django.db import models
  from datetime import date
  
  class Person(model.Model)
      "Last Name"
      last_name = models.CharField(max_length=25)
  
      "Birthday"
      birth_date = models.DateField()
  
      "City of birth"
      city_of_birth = models.CharField(max_length=25)
  </code></pre>
  
  <p>I am creating a Unique ID using these fields. Specifically, I'm conjoining parts of each field into one string variable (details below). I was able to get this to work as a Property but I don't know how to store a calculated field in the database.</p>
  
  <pre><code>"Unique ID"
  def get_id(self):
      a = self.last_name[:2].upper()     #First 2 letters of last name
      b = self.birth_date.strftime('%d')     #Day of the month as string
      c = self.city_of_birth[:2].upper()     #First 2 letters of city
      return a + b + c 
  unique_id = property(get_id)
  </code></pre>
  
  <p>I want to do a similar thing with Age. Here's what I have as a calculation:</p>
  
  <pre><code>"Age calculated from Birth Date"
  def get_age(self):
      return int((datetime.date.now() - self.birth_date.days) / 365.25)
  age = property(get_age)
  </code></pre>
  
  <p>So I'd like to store the UniqueID and Age variables in the database, as fields in the Person model. What is the best practice when doing these? Do I need to initialize the fields first, then do some sort of update query to these?</p>
  
  <p><em>Note: It is my understanding that the current code using 'property' works for rendering in the view, but it is not stored in the database.</em></p>
  
  <p>Thanks in advance! Please help me improve what I already have.</p>
  
  <p><strong>UPDATE:</strong>
  Here is code that worked for me. The problem was that I needed to drop the parentheses in the save() section, after <em>self.unique_id=self.get_unique_id</em> . It has been suggested to drop age from the database, and leave it as a property.</p>
  
  <pre><code>class Person(models.Model):
      unique_id = models.CharField(max_length=6, blank=True)
      last_name = models.CharField(max_length=25)
      birth_date = models.DateField()
      city_of_birth = models.CharField(max_length=25)
  
      @property
      def get_unique_id(self):
          a = self.last_name[:2].upper()     #First 2 letters of last name
          b = self.birth_date.strftime('%d')     #Day of the month as string
          c = self.city_of_birth[:2].upper()     #First 2 letters of city
          return a + b + c 
  
      @property
      def age(self):
          return relativedelta(self.birth_date.days, datetime.date.now()).years
  
      def save(self, *args, **kwarg):
          self.unique_id = self.get_unique_id
          super(Person, self).save(*args, **kwarg)
  
      def __str__(self):
          return self.unique_id
  </code></pre>