How to implement nested models/routes?

2019-08-06 11:42发布

I have a Playlist Model and a Track model.

class Playlist(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    user = models.ForeignKey(User, related_name="playlists")

class Track(models.Model):
    playlist = models.ForeignKey(Playlist, related_name="tracks")
    track_id = models.CharField(max_length=50)

And the serializers:

class TrackSerializer(serializers.ModelSerializer): 
    class Meta:
        model = Track
        fields = ("id", "track_id")

class PlaylistSerializer(serializers.ModelSerializer):
    user = serializers.Field(source="user.username")
    tracks = TrackSerializer(many=True)
    class Meta:
        model = Playlist
        fields = ("id", "created", "user", "tracks")

How would I go about creating views (using viewsets preferably) that allow me list a playlist's tracks at playlists/<playlist_id> and also create tracks at the same url?

I currently get non_field_errors when I go to the above url.

Couldn't find much on how to do these nested views on the docs. Thanks.

1条回答
劫难
2楼-- · 2019-08-06 12:20

You can ues drf-nested-routers (https://github.com/alanjds/drf-nested-routers).

The only interesting part in the code below is setting the track playlist in the pre_save method of the TrackViewSet.

views.py

class TrackViewSet(viewsets.ModelViewSet):
    queryset = Track.objects.all()
    serializer_class = TrackSerializer

    def pre_save(self, obj):
        obj.playlist = Playlist.objects.get(pk=self.kwargs['playlist_pk'])


class PlaylistViewSet(viewsets.ModelViewSet):
    queryset = Playlist.objects.all()
    serializer_class = PlaylistSerializer

    def pre_save(self, obj):
        obj.user = self.request.user

urls.py

from django.conf.urls import patterns, url, include

from rest_framework_nested import routers

from . import views

router = routers.SimpleRouter()

router.register(r'playlists', views.PlaylistViewSet)

playlists_router = routers.NestedSimpleRouter(router, r'playlists', lookup='playlist')
playlists_router.register(r'tracks', views.TrackViewSet)

urlpatterns = patterns('',
    url(r'^', include(router.urls)),
    url(r'^', include(playlists_router.urls)),
)
查看更多
登录 后发表回答