My little podcast backend has some models, one of which is Episode. Now there are two new ones for guests (Guest) and topics (Topic) - both of which have a ManyToManyField to Episode. This basically works as intended in the admin. Now I need to bring that to the API.
The detailed Serializer/View for each episode already has some other related data (no many-to-many so far). I also checked this post but still can't get it it work and think I might miss something.
Here's the essence just for the guest example to keep it dense, topic is literally identical:
Episode Model:
class Episode(models.Model):
number = models.CharField(max_length=10, blank=False)
title = models.CharField(max_length=100, blank=False)
show = models.ForeignKey(Show, on_delete=models.PROTECT)
created_at = models.DateTimeField(auto_now=False)
published_at = models.DateTimeField(auto_now=False)
updated_at = models.DateTimeField(auto_now=False)
cover_image = models.URLField(null=True)
def __str__(self):
return self.title
Guest Model:
class Guest(models.Model):
episodes = models.ManyToManyField(Episode)
name = models.CharField(max_length=100, blank=False)
twitter = models.CharField(max_length=20, null=True)
def __str__(self):
return self.name
Serializer:
class GuestSerializer(serializers.ModelSerializer):
class Meta:
model = Guest
fields = ('name',)
Episode Serializer where it should appear:
class EpisodeDetailSerializer(serializers.ModelSerializer):
...
guest = GuestSerializer(many=True, read_only=True)
topic = TopicSerializer(many=True, read_only=True)
class Meta:
model = Episode
fields = (..., 'guest', 'topic')
depth = 1
I have put some data on for guests and topics but I can't get them showing up on the API. I've also tried 'Guest.episodes.through' like I do have it on the admin for Inline Admin Classes but that didn't change anything.