title: using Django Rest framework to serialize custom data types and return response
body: <p>Most of the tutorials on Django Rest Framework explains using the Django models and doing CRUD operations. That is a <code>GET</code> request on user model returns the attributes of user object in JSON format if I use JSON serializer.</p>
  
  <p>I am designing my Django application to process a query and return response.
  For example, I provide a REST API to get the results of the following query</p>
  
  <p>"Get me the user first name and department whose salary than XXX"</p>
  
  <p>Here are my Django models:</p>
  
  <pre><code>class UserProfile(AbstractUser):
      age = models.PositiveIntegerField(_("age"))
      salary=models.PositiveIntegerField(_("salary"))
  
  AUTH_USER_MODEL = "profiles.UserProfile"
  User = get_user_model()
  
  class Department(models.Model):
        users=models.ForeignKey(User)
        dept_name = models.CharField(max_length=30)
  </code></pre>
  
  <p>Now I have the following DTO (Data transfer object):</p>
  
  <pre><code>class CustomResponse(object):
  
  def __init__(self, user_name, salary, dept_name):
        self.user_name = user_name
        self.salary = salary
        self.dept_name=dept_name
  </code></pre>
  
  <p>In my REST service implemented using DRF, I want the following</p>
  
  <pre><code>@api_view(['GET'])
  def getNameandDept(salary):
      users=User.objects.filter(salary__gt=salary)
      toreturn=[]
      for user in users:
          response=CustomResponse(user.first_name,user.salary,user.dept_name)
          to_return.append(response)
      return Response(to_return)
  </code></pre>
  
  <p>I am not sure what is the right way to implement the above, with the tools that Django rest framework provide.</p>
  
  <p>I am expecting the response something like this</p>
  
  <pre><code>[{user_name:"matt", salary:"5000", dept_name:"ENG"},{user_name:"smith",salary:"4000", dept_name:"HR"}....]
  </code></pre>
  
  <p>Thanks</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I was hoping DRF provides out of box tool for this kind of serialization. I have been using JAX-RS API (jersey and RESTeasy) that does this serialization. </p>