Django REST framework without model
<p>I want to use Django REST framework to create an API to call different methods. I read the guide of [django-rest-framework][1] to work with this framework, but I still have some questions.</p>

<p>I have no model I get my data from an external database. I want to try first something simple:</p>

<ul>
<li>Get all list of project</li>
<li>Get the data from one project</li>
</ul>

<p>For that I create new app I include in the setting file and in my view.py I include that for the fist case</p>

<pre><code>def connect_database():
  db = MySQLdb.connect(host='...', port=, user='...', passwd='...', db='...')
  try:
    cursor = db.cursor()
    cursor.execute('SELECT * FROM proj_cpus')
    columns = [column[0] for column in cursor.description]
    # all_rows = cursor.fetchall()
    all_rows = []
    for row in iter_row(cursor):
      all_rows.append(dict(zip(columns, row)))
  finally:
    db.close()
  return all_rows

def iter_row(cursor, size= 1000):
  while True:
    results = cursor.fetchmany(size)
    if not results:
      break
    for item_result in results:
      yield item_result


class cpuProjectsViewSet(viewsets.ViewSet):
  serializer_class = serializers.cpuProjectsSerializer

  def list(self, request):
    all_rows = connect_database()
    name_project = []  
    for item_row in all_rows:
      name_project.append(item_row['project'])
    name_project = list(sorted(set(name_project)))
    serializer = serializers.cpuProjectsSerializer(instance=name_project, many=False)
    return Response(serializer.data)
</code></pre>

<p>my serializers file I have this</p>

<pre><code>class cpuProjectsSerializer(serializers.Serializer):
  project = serializers.CharField(max_length=256)


  def update(self, instance, validated_data):
    instance.project = validated_data.get('project', instance.project)
    return instance
</code></pre>

<p>Now when I execute this <code>http://127.0.0.1:8000/hpcAPI</code></p>

<p>I obtain this error </p>

<pre><code>Got AttributeError when attempting to get a value for field `project` on serializer `cpuProjectsSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `list` instance.
Original exception text was: 'list' object has no attribute 'project'.
</code></pre>

<p>I look for in google and I change this </p>

<p><code>serializers.cpuProjectsSerializer(instance=name_project, many=False)</code> for</p>

<pre><code>serializers.cpuProjectsListSerializer(instance=name_project, many=False)
</code></pre>

<p>but I obtain the same error! </p>

<p>any idea about that!</p>

<p>Thanks in adavances</p>

<p>From docs <a href="http://www.django-rest-framework.org/tutorial/1-serialization/#creating-a-serializer-class" rel="nofollow">here</a>.You don't have to have a model for create a Serializer class.You can define some serializers field then use them. You should not import CPUProjectsViewSet and also define it in below</p>

<pre><code>from mysite.hpcAPI.serializers import CPUProjectsViewSet

class CPUProjectsViewSet(viewsets.ViewSet):
"""
return all project name
"""
    all_rows = connect_database()
</code></pre>

python django django-rest-framework