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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With