title: Django Rest Framework: Function Based API serializers
body: <p>Unsolicited for I've been put on an API-project where the requirements are:</p>
  
  <ul>
  <li>Django </li>
  <li>Django Rest Framework</li>
  <li>Python 3.5</li>
  <li>API needs to be function based (so no classes)</li>
  </ul>
  
  <p>Where the goal is an API that returns data (so I only need 'Read' from CRUD-design principles) in JSON format for internal use by displays. </p>
  
  <p>Django itself has to connect to a default database and a legacy MYSQL data (this works). I've done so with the <code>settings.py</code> file. </p>
  
  <p>I then created a new application called <code>museum_api</code>; this works as well.</p>
  
  <p>After that I built my models of the legacy database with the help of <code>python manage.py inspectdb --database=museum_data &gt; models.py</code> 
  the above code generated a python file with classes for every table in the MYSQL database in the rootfolder called 'musart' of the project (this folder holds: manage.py, musart and museum_api.)</p>
  
  <p>Then I created a static JSON response by going inside of the folder called museum_api and created a file: <code>views.py</code> and <code>urls.py</code></p>
  
  <p>I left these empty for a sec and went back to the root-folder and into the inner-project folder to edit the urls.py file in there. In that file I added: </p>
  
  <pre><code>url(r'', include('museum_api.urls')), 
  </code></pre>
  
  <p>The intent is that the API is the first thing people encounter when visiting that subdomain. This too works fine.</p>
  
  <p>Then I went back to the folder <code>museum_api</code> and edited the <code>views.py</code> file to create two static JSON responses as a test: </p>
  
  <pre><code>from rest_framework import status, renderers
  from rest_framework.decorators import api_view
  from rest_framework.response import Response
  from django.http import HttpResponse
  from django.utils.encoding import smart_text
  
  @api_view(['GET'])
  def view(request):
      return Response({"name": "HERMÄNN", "test":"mytest"})
  
  @api_view(['GET'])
  def hello_world(request):
      return Response({"message": "Hello, world!"})
  
  def index(request):
      return HttpResponse("""Hello, httpresonse is here!""")
  </code></pre>
  
  <p>Next up I edited the <code>urls.py</code> file inside: museum_api:
      from django.conf.urls import url
      from . import views</p>
  
  <pre><code>urlpatterns = [
      url(r'^view$', views.view, name='view'),
      url(r'^hello$', views.hello_world, name='hello_world'),
      url(r'^$', views.index, name='index'),
  ]
  </code></pre>
  
  <p>Up until this point I understood all of the documentation; and whenever I run the server and go to the url I get what I expect either the Indexfile, or two JSON responses.</p>
  
  <p>For the past few days I've been stuck with making the API interactive... which is the whole point of the project... All projects and tutorials I found use classes, which violate one of the core requirements. </p>
  
  <p>At this stage I have only questions, no working code...</p>
  
  <p>1) The next step would be to create one function per api-method. An example of this is a function to query all artist with an api method called: <code>artist</code>. One of the parameters it should take is 'country'. A possible way of requesting the data to the api is: <code>url?artist;format=json;country=france</code>
  I'd think of something like this (pseudocode):</p>
  
  <pre><code>def artist_api(request):
      data = select * from artists where country = 'france';
      return (data)
  </code></pre>
  
  <p>2) In museum_api > views.py: Why do I need to put an <code>@api_view['GET']</code> before every new function? If I leave it out, that function stops working.</p>
  
  <p>3) How do I write my serializers so that if the api gets a request (such as with the country example) the python script know what database(s) to query and to return the correct data?</p>
  
  <p>4) Related to 3: Where do I need to create a serializers.py script? In the root folder where my models.py is? Or in the application folder?</p>
  
  <p>Sorry for the long question.
  Best regards</p>