Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to work with ManytoManyField with through relation in django-rest-framework

Tags:

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?

like image 829
Nitin Avatar asked Dec 25 '12 17:12

Nitin


People also ask

How do I pass Queryset to serializer?

To serialize a queryset or list of objects instead of a single object instance, you should pass the many=True flag when instantiating the serializer. You can then pass a queryset or list of objects to be serialized.

How do you implement many-to-many relationships in Django?

To define a many-to-many relationship, use ManyToManyField . What follows are examples of operations that can be performed using the Python API facilities. You can't associate it with a Publication until it's been saved: >>> a1.

How do you pass extra context data to Serializers in Django REST Framework?

In function based views we can pass extra context to serializer with "context" parameter with a dictionary. To access the extra context data inside the serializer we can simply access it with "self. context". From example, to get "exclude_email_list" we just used code 'exclude_email_list = self.


1 Answers

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.

like image 128
Arial Avatar answered Sep 21 '22 08:09

Arial