title: Python Django Rest - Return extra field with lowest, highest and average values of some other field
body: <p>I'm new to Django and APIs in general and I want to create a Django based API using Django Rest Framework.</p>
  <p>Here's what I want to do:</p>
  <p>Endpoint to age range report:</p>
  <pre><code>curl -H 'Content-Type: application/json' localhost:8000/reports/employees/age/
  </code></pre>
  <p>Response:</p>
  <pre><code>{
      &quot;younger&quot;: {
          &quot;id&quot;: &quot;1&quot;,
          &quot;name&quot;: &quot;Anakin Skywalker&quot;,
          &quot;email&quot;: &quot;skywalker@ssys.com.br&quot;,
          &quot;department&quot;: &quot;Architecture&quot;,
          &quot;salary&quot;: &quot;4000.00&quot;,
          &quot;birth_date&quot;: &quot;01-01-1983&quot;},
      &quot;older&quot;: {
          &quot;id&quot;: &quot;2&quot;,
          &quot;name&quot;: &quot;Obi-Wan Kenobi&quot;,
          &quot;email&quot;: &quot;kenobi@ssys.com.br&quot;,
          &quot;department&quot;: &quot;Back-End&quot;,
          &quot;salary&quot;: &quot;3000.00&quot;,
          &quot;birth_date&quot;: &quot;01-01-1977&quot;},
      &quot;average&quot;: &quot;40.00&quot;
  }
  </code></pre>
  <p>Endpoint to salary range report:</p>
  <pre><code>curl -H 'Content-Type: application/json' localhost:8000/reports/employees/salary/
  </code></pre>
  <p>Response:</p>
  <pre><code>{
      &quot;lowest &quot;: {
          &quot;id&quot;: &quot;2&quot;,
          &quot;name&quot;: &quot;Obi-Wan Kenobi&quot;,
          &quot;email&quot;: &quot;kenobi@ssys.com.br&quot;,
          &quot;department&quot;: &quot;Back-End&quot;,
          &quot;salary&quot;: &quot;3000.00&quot;,
          &quot;birth_date&quot;: &quot;01-01-1977&quot;},
      &quot;highest&quot;: {
          &quot;id&quot;: &quot;3&quot;,
          &quot;name&quot;: &quot;Leia Organa&quot;,
          &quot;email&quot;: &quot;organa@ssys.com.br&quot;,
          &quot;department&quot;: &quot;DevOps&quot;,
          &quot;salary&quot;: &quot;5000.00&quot;,
          &quot;birth_date&quot;: &quot;01-01-1980&quot;},
      &quot;average&quot;: &quot;4000.00&quot;
  }
  </code></pre>
  <p>I have two apps, <code>employees</code> and <code>reports</code>.</p>
  <p>Here's <strong>employees/models.py</strong>:</p>
  <pre><code>class Employee(models.Model):
      name = models.CharField(max_length=250, default='FirstName LastName')
      email = models.EmailField(max_length=250, default='employee@email.com')
      departament = models.CharField(max_length=250, default='Full-Stack')
      salary = models.DecimalField(max_digits=15, decimal_places=2, default=0)
      birth_date = models.DateField()
  </code></pre>
  <p>Here's <strong>employees/serializers.py</strong>:</p>
  <pre><code>class EmployeeSerializer(serializers.ModelSerializer):
      class Meta:
          model = Employee
          fields = ['id', 'name', 'email', 'departament', 'salary', 'birth_date']
  </code></pre>
  <p>I'm not sure how to create my views and serializers for my report app. How should I approach this?</p>
  <p>How can I return an extra field with a calculation between values of another field? Should I create a custom field in my serializer?</p>
  <p>By reading the <a href="https://docs.djangoproject.com/en/dev/topics/db/aggregation/" rel="nofollow noreferrer">docs</a> I've figured that the query I should use should be something like:</p>
  <pre><code>Employee.objects.aggregate(Max(&quot;salary&quot;), Min(&quot;salary&quot;), Avg(&quot;salary&quot;))
  </code></pre>
  <p>Or maybe, for salary for example:</p>
  <pre><code>Employee.objects.all().order_by('salary')
  </code></pre>
  <p>But how would I go about using it?</p>
  <p>I don't know where to use or how to go about it as my understanding of APIs and Django Rest Framework is still very lacking.</p>
  <p>Should it go in my <strong>reports/view.py</strong>? Or in my <strong>employees/serializers.py</strong>?</p>
  <p>Should I create a <strong>reports/serializers.py</strong> file?</p>
  <p>Do I need a <strong>reports/model</strong> class with <code>lowest</code>, <code>highest</code>, <code>average</code> fields plus an employee object field?</p>
  <p>Should I override the <code>list()</code> function of my <code>ReportSalaryListAPIView</code> class in my <strong>reports/views.py</strong>? (This class doesn't exist yet)</p>
  <p>I'm very lost and confused. Please help point me in the right direction.</p>
  <p>Thank you in advance.</p>
  <hr />
  <p><strong>Edit:</strong></p>
  <p>My <strong>employees/model.py</strong> now looks like this:</p>
  <pre><code>class Employee(models.Model):
      name = models.CharField(max_length=250, default='FirstName LastName')
      email = models.EmailField(max_length=250, default='employee@email.com')
      departament = models.CharField(max_length=250, default='Full-Stack')
      salary = models.DecimalField(max_digits=15, decimal_places=2, default=0)
      birth_date = models.DateField()
  
      @property
      def get_age(self):
          delta = relativedelta(self.birth_date.days, datetime.today()).years
          return delta
  
      def save(self, *args, **kwargs):
          self.age = self.get_age()
          super(Employee, self).save(*args, **kwargs)
  </code></pre>
  <p>My <strong>employees/serializers.py</strong> now:</p>
  <pre><code>class EmployeeSerializer(serializers.ModelSerializer):
  
      class Meta:
          model = Employee
          fields = ['id', 'name', 'email', 'departament', 'salary', 'birth_date', 'age']
  </code></pre>
  <p>I've run the <code>makemigrations</code> and <code>migrate</code> commands with <code>manager.py</code>, just in case.</p>
  <p>But I'm now running into this error when trying to create a new employee:</p>
  <pre><code>'datetime.date' object has no attribute 'days'
  </code></pre>
  <p>What is happening?</p>