21

Basically I have models like this:

class Playlist(models.Model):
    key = models.CharField(max_length=255,blank=True, unique=True)
    user = models.ForeignKey(User)
    title = models.CharField(max_length=200)
    pub_date = models.DateTimeField(auto_now_add=True)
    videos = models.ManyToManyField(Video, through='PlaylistVideo')

class PlaylistVideo(models.Model):
    playlist = models.ForeignKey(Playlist)
    video =    models.ForeignKey(Video)
    position = models.IntegerField()

class Video(models.Model):
    title = models.CharField(max_length=255,blank=True)
    description = models.TextField(blank=True)
    thumb =  models.URLField(blank=True)
    duration = models.IntegerField(default=0)

Now I want an API to return PLAYLISTS like this... But Videos should be sorted by POSITION in PlaylistVideo Model

{
        "key": "h8x3",
        "title": "First Playlist",
        "pub_date": "2012-10-11T17:00:26Z",
        "videos": [
            {
                ....
            },
            {
                ....
            }
        ]
    },

How should I got about it?

Max Candocia
  • 4,294
  • 35
  • 58
Nitin
  • 738
  • 6
  • 19

3 Answers3

8

Not too sure if you've managed to solve your issue, but I've came across this myself, and managed to make it work like by doing something like this:

create a custom serializer like this:

class PlaylistVideoSerializer(serializers.HyperlinkedModelSerializer):
    title = serializers.ReadOnlyField(video.title)
    description = serializers.ReadOnlyField(video.description)
    thumb = serializers.ReadOnlyField(video.thumb)
    duration = serializers.ReadOnlyField(video.duration)

    class Meta:
        # your associative entity here
        model = PlaylistVideo
        fields = ('title', 'description', 'thumb', 'duration')

Here, I'm assuming you'd like to display all the fields under videos entity/table. Feel free to adjust to your needs/likings.

Then, all you need to do is this

class PlaylistSerializer(serializers.ModelSerializer):
    videos = PlaylistVideoSerializer(source='playlistvideo_set', many=True)
    class Meta: 
        model = Playlist
        fields = ('key', 'title', 'pub_dates', 'videos')

Note: Always make sure that the source is referencing to the associative entity. Else you'd get a list of empty json.

Hope this would help anyone out there facing similar issue.

Arial
  • 4,844
  • 2
  • 18
  • 17
  • Any ideas on how to modify if you want to be able to create and/or update Videos via Playlists? It doesn't seem to work with this set up. – getup8 Dec 28 '16 at 06:57
  • 1
    Answered my own question. You need your own create and update methods and you need to use non-ReadOnlyFields. – getup8 Dec 28 '16 at 07:52
  • 1
    Remember: Make your create and update methods in viewset, you'll get to read the POST from there. – Enderson Menezes Jul 15 '19 at 18:35
  • I'm getting error on passing ReadOnlyField a parameter. dev_name = serializers.ReadOnlyField(LightDevice.dev_name) TypeError: __init__() takes 1 positional argument but 2 were given. Any Help? – trebew Mar 10 '23 at 13:15
  • Found it. It must be a named argument. serializers.ReadOnlyField(source='lightdevice.dev_name') – trebew Mar 10 '23 at 13:23
4

You can do it like this:

class PlaylistVideoList(generics.ListCreateAPIView):
    serializer_class = PlaylistVideoSerializer
    queryset = PlaylistVideo.objects.all().order_by('position')

in serializers.py:

class PlaylistVideoSerializer(serializers.ModelSerializer):
    class Meta:
        model = PlaylistVideo
cor
  • 3,323
  • 25
  • 46
3

We need to add some documentation on 'through' relationships really.

In the meantime, this discussion may help:

https://groups.google.com/forum/#!topic/django-rest-framework/xzOhjILq3xA/discussion

Tom Christie
  • 33,394
  • 7
  • 101
  • 86