I have the following three model definitions
class Program(models.Model):
title = models.CharField(max_length=200, null=True, blank=True)
assigned_by = models.ForeignKey('physio.Physio', null=True, blank=True)
assigned_to = models.ManyToManyField('patient.Patient')
start_date = models.DateTimeField(null=True, blank=True)
end_date = models.DateTimeField(null=True, blank=True)
duration = models.CharField(max_length=200, null=True, blank=True)
description = models.CharField(max_length=200, null=True, blank=True)
exercises = models.ManyToManyField(
'exercise.Exercise')
Here is the serialiser
class ProgramSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Program
fields = (
'title', 'assigned_by', 'start_date', 'end_date', 'duration', 'description', 'exercises'
)
Following is the views :
class ProgramViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = Program.objects.all()
serializer_class = ProgramSerializer
I want to Get all programs assigned by a particular physio to a patient. How should the views be altered?
The URL can be - /programs/physio/:physioID/patient/:patientID
I have used routers for the generic list and detail views.
You can implement .get_queryset()
method of ProgramViewSet
def get_queryset(self):
physio = Physio.objects.get(id=self.kwargs['physioID'])
patient = Patient.objects.get(id=self.kwargs['patientID'])
return Purchase.objects.filter(assigned_by=physio,
assigned_to=patient)
As described in the documentation .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.