Skip to content

Instantly share code, notes, and snippets.

@court-jus
Last active August 29, 2015 14:21
Show Gist options
  • Save court-jus/d787537c9581193cf02e to your computer and use it in GitHub Desktop.
Save court-jus/d787537c9581193cf02e to your computer and use it in GitHub Desktop.
HTTP 200 OK
Content-Type: application/json
Vary: Accept
Allow: GET, POST, HEAD, OPTIONS
[
{
"id": 87,
"nom": "Panel 200",
"analyseur": {
"id": 200,
"automate": "13644",
"nom": "Analyseur 1"
}
},
.......
]
# sent to /qc/api/panels/ via the RESTframework html interface
{
"nom": "test",
"analyseur": {
"id": 104,
"automate": "Advia 1800",
"nom": "Advia 1800"
}
}
HTTP 400 BAD REQUEST
Content-Type: application/json
Vary: Accept
Allow: GET, POST, HEAD, OPTIONS
{
"analyseur": {
"nom": [
"This field must be unique."
],
"automate": [
"This field must be unique."
]
}
}
# -*- coding: utf-8 -*-
from rest_framework import serializers, viewsets, routers
from qc.models import Panel
from automate.models import Analyseur
class AnalyseurSerializer(serializers.ModelSerializer):
class Meta:
model = Analyseur
fields = ("id", "automate", "nom")
class PanelSerializer(serializers.ModelSerializer):
analyseur = AnalyseurSerializer()
class Meta:
model = Panel
fields = ("id", "nom", "analyseur")
class PanelViewSet(viewsets.ModelViewSet):
queryset = Panel.objects.select_related()
serializer_class = PanelSerializer
router = routers.DefaultRouter()
router.register(r'panels', PanelViewSet)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment