Django Rest Framework API by accept multiple input and output multiple data
<p>I am two months with Django and API Rest Framework.
I am developing an API to accept (userid and week_number), and output with (week_number and status).</p>

<p>This is my model:</p>

<pre><code>class Timesheet(models.Model):
    userid = models.ForeignKey(User)
    week_number = models.CharField(max_length=2)
    status = models.CharField(max_length=255)
</code></pre>

<p>This is my complete data example:</p>

<pre><code> [
    {
        "id": 1,
        "userid": 1,
        "week_number": "32",
        "status": "completed"
    },
    {
        "id": 2,
        "userid": 1,
        "week_number": "33",
        "status": "approved"
    },
    {
        "id": 3,
        "userid": 1,
        "week_number": "34",
        "status": "incomplete"
    }
]
</code></pre>

<p>API I want is like this:</p>

<pre><code>ACCEPT: userid=1
ACCEPT: week_number=33
ACCEPT: week_number=34
OUTPUT: week_number=33,status="approved"
OUTPUT: week_number=34,status="incomplete"
</code></pre>

<p>Note: I do not want data about <code>week_number=32</code></p>

<p>I not sure how to code for this kind of API. Hope someone can help.</p>

<p>This would be the general route to getting what you want. </p>

<p>in models.py</p>

<pre><code>class Timesheet(models.Model):
    userid = models.ForeignKey(User)
    week_number = models.CharField(max_length=2)
    status = models.CharField(max_length=255)
</code></pre>

<p>in serializer.py:</p>

<pre><code>class TimesheetSerializer(serializers.ModelSerializer):
    class Meta:
        model = Timesheet
        // specify the fields that you want API to return below
        fields = ('week_number', 'status')
</code></pre>

<p>in views.py:</p>

<pre><code>from rest_framework import generics    

class TimesheetListView(generics.ListAPIView):
        serializer_class = TimesheetSerializer

        def get_queryset(self):
            //get user and week_number from GET request
            user = self.request.GET['user']
            week_number = self.request.GET['week']

            //filter and return the first result where user=user and week=week
            return Timesheet.objects.filter(user=user, week_number=week_number)
</code></pre>

<p>in urls.py:</p>

<pre><code>url(r'^api/timesheet$', views.TimesheetListView.as_view(), name='timesheet'),
</code></pre>

<p>you can then access this endpoint with e.g.:  </p>

<p><a href="http://127.0.0.1:8000/api/timesheet?user=123&amp;week=33&amp;format=api" rel="nofollow">http://127.0.0.1:8000/api/timesheet?user=123&amp;week=33&amp;format=api</a></p>

<p>It's good practice to disable Browsable API in production. it can be done by the following code in settings.py(this way you won't have to use &amp;format=api in your call above):</p>

<pre><code>REST_FRAMEWORK = {
    other settings ...
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
    )
    other settings ...
}
</code></pre>

<p>and in development mode you can do in settings.py:</p>

<pre><code>    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
)
</code></pre>

<p>I didn't get the time to test this but this is the general guidelines of achieving what you want. Do add the required exception-handling to this as well. 
you should go through the Django DRF tutorial though if you haven't already: <a href="http://www.django-rest-framework.org/tutorial/quickstart/" rel="nofollow">http://www.django-rest-framework.org/tutorial/quickstart/</a></p>

python django django-rest-framework