-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom serializer ref
- Loading branch information
Showing
13 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class WebEndpointsConfig(AppConfig): | ||
name = 'web_endpoints' |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from rest_framework import serializers | ||
|
||
from web_db.models import Album, Song | ||
|
||
|
||
class BasicSongSerializer(serializers.ModelSerializer): | ||
|
||
class Meta: | ||
model = Song | ||
fields = ('title', 'track', 'duration',) | ||
|
||
|
||
class AlbumSerializer(serializers.ModelSerializer): | ||
songs = BasicSongSerializer(many=True, source='song_set') | ||
|
||
class Meta: | ||
model = Album | ||
fields = ('title', 'artist', 'year', 'songs') | ||
|
||
def create(self, data): | ||
songs = data.pop('song_set') | ||
album = Album.objects.create(**data) | ||
for song in songs: | ||
Song.objects.create(album=album, **song) | ||
return album | ||
|
||
|
||
class SongSerializer(serializers.ModelSerializer): | ||
album = AlbumSerializer() | ||
|
||
class Meta: | ||
model = Song | ||
fields = ('title', 'track', 'duration', 'album',) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.conf.urls import url, include | ||
from web_endpoints.views import AlbumViewSet, SongViewSet | ||
from rest_framework.routers import DefaultRouter | ||
|
||
router = DefaultRouter() | ||
router.register(r'albums', AlbumViewSet) | ||
router.register(r'songs', SongViewSet) | ||
|
||
urlpatterns = [ | ||
url(r'', include(router.urls)), | ||
] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from rest_framework import viewsets | ||
|
||
from web_db.models import Album, Song | ||
|
||
from web_endpoints.serializers import AlbumSerializer, SongSerializer | ||
|
||
|
||
class AlbumViewSet(viewsets.ModelViewSet): | ||
queryset = Album.objects.all() | ||
serializer_class = AlbumSerializer | ||
|
||
|
||
class SongViewSet(viewsets.ModelViewSet): | ||
queryset = Song.objects.all() | ||
serializer_class = SongSerializer |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
Django==1.11.4 | ||
ipython==6.1.0 | ||
djangorestframework==3.6.4 | ||
ipdb==0.10.3 |