API REST

En esta tarea añadiremos una API REST a nuestra aplicación, siguiendo el tutorial de la página Django + MongoDB = Django REST Framework Mongoengine, y la documentación de Django Rest Framework Mongoengine


$ pip install djangorestframework
$ pip install django-rest-framework-mongoengine

y las añadimos a settings.py
INSTALLED_APPS = (
  ...,
  'registration',
  'rest_framework',
  'rest_framework_mongoengine',
  'restaurantes',
)

y hacemos un nuevo archivo serializers.py para el API, más o menos:
   from rest_framework_mongoengine import serializers
   from rest_framework_mongoengine import viewsets

   from .models import restaurants

   class restaurantsSerializer(serializers.DocumentSerializer):

       class Meta:
           model = restaurants
           fields = ('name', 'cuisine', 'borough', 'address', 'image')


   class restaurantsViewSet(viewsets.ModelViewSet):
       lookup_field = 'name'
       serializer_class = restaurantsSerializer

       def get_queryset(self):
           return restaurants.objects.all()


y nos fañtaría añadir las rutas para el API en urls.py
   from rest_framework_mongoengine import routers

   from . import views
   from . import serializers

   # this is DRF router for REST API viewsets
   router = routers.DefaultRouter()

   # register REST API endpoints with DRF router
   router.register(r'restaurants', serializers.restaurantsViewSet, r"restaurants")


   urlpatterns = [
            ...
              # REST API root view (generated by DRF router)
              url(r'^api/', include(router.urls, namespace='api')),

   ]