Commit cbf7a2b1 authored by luxcem's avatar luxcem

adds api for articles

parent c070ee3a
from rest_framework import routers
class DefaultRouter(routers.DefaultRouter):
"""
Extends `DefaultRouter` class to add a method for extending url
routes from another router. # from
http://stackoverflow.com/questions/31483282/django-rest-framework-combining-routers-from-different-apps
"""
def extend(self, router):
"""
Extend the routes with url routes of the passed in router.
Args:
router: SimpleRouter instance containing route definitions.
"""
self.registry.extend(router.registry)
from rest_framework import serializers
from rp.models import Article
class ArticleSerializer(serializers.ModelSerializer):
class Meta:
model = Article
fields = "__all__"
from rest_framework import routers
from .views import ArticleViewSet
router = routers.DefaultRouter()
router.register(r"articles", ArticleViewSet)
from rest_framework import viewsets
from rp.models import Article
from .serializers import ArticleSerializer
class ArticleViewSet(viewsets.ModelViewSet):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
......@@ -5,7 +5,15 @@ rp_new URL Configuration
from django.conf.urls import url, include
from django.contrib import admin
# Django rest framework router
from core.routers import DefaultRouter
from rp.api.urls import router as rp_router
router = DefaultRouter()
router.extend(rp_router)
urlpatterns = [
url(r"^admin/", admin.site.urls),
url(r"^api/", include(router.urls, namespace="api")),
url(r"^rp/", include("rp.urls"))
]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment