Hi I have a model like:
class Appointment(models.Model):
hospital = models.ForeignKey(Hospital, on_delete=models.CASCADE)
patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
My View looks like:
class AppointmentViewSet(viewsets.ModelViewSet):
queryset = Appointment.objects.all()
serializer_class = AppointmentSerializer
In my urls:
router.register(r'appointments', AppointmentViewSet)
Now I want to filter the list of appointments by some patient id. This id should be given by the requester through url. I'm thinking about using kwargs to catch it. But I have no idea how to do it. I know I have to override the list method.
def list(self, request, *args, **kwargs):
# what do I write here? so that the queryset would be filtered by patient id sent through the url?
How do I customize the url and/or the view to accommodate the patient id parameter? I just want to modify the list request, all other actions(create, details, destroy) should be handled by the modelviewset's default behavior.
Thanks.