How to work with ManytoManyField with through rela

2019-03-23 08:03发布

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?

3条回答
Summer. ? 凉城
2楼-- · 2019-03-23 08:47

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.

查看更多
Fickle 薄情
3楼-- · 2019-03-23 08:50

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
查看更多
相关推荐>>
4楼-- · 2019-03-23 09:09

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

查看更多
登录 后发表回答