I'm trying to do a REST in Django using the VIEWSETS library. I created a Container model that works well. Container is associated with the ContainerModerator model. The endpoint should be:
POST containers/{container_id}/moderators
One of the fields in ContainerModerator is container_id. I would like to get it from the path.
Below is what I have now. I tried in different ways to get there. I also read similar posts, usually for a library other than VIEWSETS. I am a beginner and I wonder if there is a simple, elegant solution for this case that will be easy for me to understand? Should I use other libraries like APIVIEW or GenericAPIView?
models:
class ContainerModerator(models.Model):
moderator_id = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
container_id = models.ForeignKey(Container, on_delete=models.CASCADE)
serializers:
class ContainerModeratorSerializer(serializers.ModelSerializer):
class Meta:
model = models.ContainerModerator
fields = '__all__'
views:
class ContainerModeratorViewSet(viewsets.ModelViewSet):
serializer_class = serializers.ContainerModeratorSerializer
queryset = models.ContainerModerator.objects.all()
def perform_create(self, serializer):
serializer.save()
urls:
router.register('v1/containers/<int:container_id>/moderators',
views.ContainerModeratorViewSet)
urlpatterns = [
path('', include(router.urls))
]