Created
September 4, 2013 01:28
-
-
Save gabrielgrant/6431778 to your computer and use it in GitHub Desktop.
Arrrg, shouldn't django-rest-framework `viewsets.ModelViewSet` use `serializers.ModelSerializer` by default? It seems to be using `HyperlinkedModelSerializer` instead. Do I really need to create and specify a custom serializer just to get an ID output rather than a hyperlink?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"url": "http://localhost:8000/api/v1/choices/1/", | |
"poll": "http://localhost:8000/api/v1/polls/1/", | |
"choice_text": "Pretty confusing", | |
"votes": 0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"id": 1, | |
"question": "How confusing is this?", | |
"pub_date": "2013-09-03T20:59:32.952Z" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf.urls import patterns, include, url | |
from django.views.generic import TemplateView | |
from rest_framework import viewsets, routers, serializers | |
from polls.models import Poll, Choice | |
class PollSerializer(serializers.ModelSerializer): | |
class Meta: | |
model = Poll | |
# ViewSets define the view behavior. | |
class PollViewSet(viewsets.ModelViewSet): | |
model = Poll | |
serializer_class = PollSerializer | |
class ChoiceViewSet(viewsets.ModelViewSet): | |
model = Choice | |
router = routers.DefaultRouter() | |
router.register(r'polls', PollViewSet) | |
router.register(r'choices', ChoiceViewSet) | |
urlpatterns = patterns('', | |
url(r'^$', TemplateView.as_view(template_name='polls/index.html')), | |
url(r'^api/v1/', include(router.urls)), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sigh....DEFAULT_MODEL_SERIALIZER_CLASS setting