The goal I am trying to achieve is querying for all of a specific Model that has a ForeignKey relationship with an individual instance of another Model.
I'm curious what the proper way of routing an API call would look like. The thought/hope is that it will look similar to:
'/api/v1/devices/<id>/breadcrumbs'
I am new to Django Rest Framework and am unsure of what to do here. I have created basic Viewsets like below:
class DeviceViewSet(viewsets.ModelViewSet):
queryset = Device.objects.all()
serializer_class = DeviceSerializer
class BreadcrumbViewSet(viewsets.ModelViewSet):
queryset = Breadcrumb.objects.all()
serializer_class = BreadcrumbSerializer
As well as the basic routes:
router.register(r'devices', DeviceViewSet)
router.register(r'breadcrumbs', BreadcrumbViewSet)
And the Breadcrumb model:
class Breadcrumb(models.Model):
timestamp = models.DateTimeField(
auto_now_add=True,
editable=False,
null=False,
blank=False
)
device = models.ForeignKey(
Device,
on_delete=models.CASCADE
)
But don't know how to extend this to the logic I am looking to achieve.