Skip to content

Instantly share code, notes, and snippets.

@gabrielgrant
Created September 4, 2013 01:28
Show Gist options
  • Save gabrielgrant/6431778 to your computer and use it in GitHub Desktop.
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?
{
"url": "http://localhost:8000/api/v1/choices/1/",
"poll": "http://localhost:8000/api/v1/polls/1/",
"choice_text": "Pretty confusing",
"votes": 0
}
{
"id": 1,
"question": "How confusing is this?",
"pub_date": "2013-09-03T20:59:32.952Z"
}
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)),
)
@gabrielgrant
Copy link
Author

Sigh....DEFAULT_MODEL_SERIALIZER_CLASS setting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment