Skip to content

Commit

Permalink
Merge pull request #14 from agiliq/review_of_doc
Browse files Browse the repository at this point in the history
Review of doc
  • Loading branch information
yashrastogi16 committed Mar 15, 2018
2 parents 0120311 + 6c14a09 commit c2fa3f8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
10 changes: 5 additions & 5 deletions docs/serailizers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The first thing we need for our API is to provide a way to serialize model insta
Creating Serializers
-----------------------

Lets get started with creating serializer classes which will serialize and deserialize the model instances to json representations. Create a file named :code:`polls/serializers.py`. We will use :code:`ModelSerializer` which will reduce code duplication by automatically determing the set of fields and by creating implementations of the create() and update() methods.
Lets get started with creating serializer classes which will serialize and deserialize the model instances to json representations. Create a file named :code:`polls/serializers.py`. We will use :code:`ModelSerializer` which will reduce code duplication by automatically determing the set of fields and by creating implementations of the :code:`create()` and :code:`update()` methods.

Our :code:`polls/serializers.py` looks like this.

Expand Down Expand Up @@ -67,8 +67,8 @@ What have we got with this? The :code:`PollSerializer` class has a number of met

* A :code:`is_valid(self, ..)` method which can tell if the data is sufficient and valid to create/update a model instance.
* A :code:`save(self, ..)` method, which khows how to create or update an instance.
* A code:`create(self, validated_data, ..)` method which knows how to create an instance. This method can be overriden to customize the create behaviour.
* A code:`update(self, instance, validated_data, ..)` method which knows how to update an instance. This method can be overriden to customize the update behaviour.
* A :code:`create(self, validated_data, ..)` method which knows how to create an instance. This method can be overriden to customize the create behaviour.
* A :code:`update(self, instance, validated_data, ..)` method which knows how to update an instance. This method can be overriden to customize the update behaviour.


Using the :code:`PollSerializer`
Expand All @@ -78,7 +78,7 @@ Let's use the serializer to create a :code:`Poll` object.

.. code-block:: ipython
In [1]: from polls.serialzers import PollSerializer
In [1]: from polls.serializers import PollSerializer
In [2]: from polls.models import Poll
Expand All @@ -93,7 +93,7 @@ Let's use the serializer to create a :code:`Poll` object.
Out[6]: 5
The :code:`poll.pk` line tells us that the object has been commited to the DB. You can also use the serializer to update a :code:`Poll` object.::
The :code:`poll.pk` line tells us that the object has been commited to the DB. You can also use the serializer to update a :code:`Poll` object. ::


In [9]: poll_serializer = PollSerializer(instance=poll, data={"question": "Mojito, Caipirinha or margarita?", "created_by": 1})
Expand Down
14 changes: 10 additions & 4 deletions docs/views-and-generic-views.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@ Creating Views with :code:`APIView`
-----------------------------------------


To start with, we will use the :code:`APIView` to build the polls list and poll detail API we built in the chapter, doc:apis-without-drf:.
To start with, we will use the :code:`APIView` to build the polls list and poll detail API we built in the chapter, :doc:`apis-without-drf`.

Add this to a new file :code:`polls/apiviews.py`

.. code-block:: python
from rest_framework.views import APIView
from rest_framework.response import Response
from django.shortcuts import get_object_or_404
from .models import Poll, Choice
from .serializers import PollSerializer
class PollList(APIView):
def get(self, request):
polls = Poll.objects.all()[:20]
data = PollSerializer(polls, many=True).data
return Response(data)
class PollDetail(APIView):
def get(self, request, pk):
poll = get_object_or_404(Poll, pk=pk)
Expand Down Expand Up @@ -75,7 +81,7 @@ Using DRF generic views to simplify code
-----------------------------------------


The :code:`PollList` and :code:`PollDetail` get the work done, but there are bunch of common operations, we can abstract away.
The :code:`PollList` and :code:`PollDetail` get the work done, but there are bunch of common operations, we can do it in abstract away.

The generic views of Django Rest Framework help us in code reusablity. They infer the response format and allowed methods from the serilizer class and base class.

Expand Down Expand Up @@ -211,7 +217,7 @@ Conect the new apiviews to urls.py.
There is a lot going on here, let us look at the attributes we need to override or set.

- :code:`queryset`: This determines the initial queryset. The queryset can be fulter filtered, sliced or ordered by the view.
- :code:`queryset`: This determines the initial queryset. The queryset can be further filtered, sliced or ordered by the view.
- :code:`serializer_class`: This will be used for validating and deserializing the input and for serializing the output.

We have used three different classes from :code:`rest_framework.generic`. The names of the classes are representative of what they do, but lets quickly look at them.
Expand Down

0 comments on commit c2fa3f8

Please sign in to comment.