Browse Source

Add basic DRF endpoint.

feature/bike-tracking
Drew Larson 8 years ago
parent
commit
04376ff35e
  1. 8
      bikeshop_project/bike/serializers.py
  2. 8
      bikeshop_project/bike/urls.py
  3. 9
      bikeshop_project/bike/views.py
  4. 8
      bikeshop_project/bikeshop/settings/base.py

8
bikeshop_project/bike/serializers.py

@ -0,0 +1,8 @@
from rest_framework import serializers
from bike.models import Bike
class BikeSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Bike

8
bikeshop_project/bike/urls.py

@ -1,7 +1,13 @@
from django.conf.urls import url
from django.conf.urls import url, include
from rest_framework import routers
from bike import views
from .views import BikesView
router = routers.DefaultRouter()
router.register(r'bikes', views.BikeViewSet)
urlpatterns = [
url(r'^api/', include(router.urls, namespace='api')),
url(r'^$', BikesView.as_view(), name='bikes')
]

9
bikeshop_project/bike/views.py

@ -1,6 +1,10 @@
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic import TemplateView
from rest_framework import viewsets
from bike.models import Bike
from bike.serializers import BikeSerializer
@method_decorator(login_required, name='dispatch')
@ -9,3 +13,8 @@ class BikesView(TemplateView):
def get(self, request):
return self.render_to_response({})
class BikeViewSet(viewsets.ModelViewSet):
queryset = Bike.objects.all()
serializer_class = BikeSerializer

8
bikeshop_project/bikeshop/settings/base.py

@ -161,3 +161,11 @@ HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
LOGIN_REDIRECT_URL = 'home'
LOGIN_URL = 'login'
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}

Loading…
Cancel
Save