I have the following two Django models (simplified for this example).
class Participant(models.Model):
name = models.CharField()
study_id = models.IntegerField()
...
# Lots of other fields
class Message(models.Model):
text = models.CharField()
participant = models.ForeignKey('Participant')
Using Django Rest Framework I have set up a ModelSerializer and ModelViewSet for each model. Using the @detail_route and @list_route decorators I have set up the following URLs.
/api/participant/ (GET,POST for list and create)
/api/participant/:study_id (GET,PUT for retrieve and update)
/api/participant/:study_id/messages (GET for list POST to send message)
I would like to add the following url for changing message meta data. This will make working with the API much easier in restangular
/api/participant/:study_id/messages/:msg_id (PATCH partial update of message)
It would be nice to have a way to embed a Message ViewSet inside the Participant since this would make the logic for GET and POST on messages simpler as well. Is this possible? If not how can I pass the msg_id to a @detail_rouet on messages?
This is a case of nested routers. Beside the one already mentioned, I will name https://github.com/chibisov/drf-extensions
Personally, I've used Restangular with drf-extensions. Comes with caching support.
Two hints on how to enable nested routing:
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