Has*_*bal 6 python django url keyword-argument django-rest-viewsets
嗨,我有一个模型,如:
class Appointment(models.Model):
hospital = models.ForeignKey(Hospital, on_delete=models.CASCADE)
patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
Run Code Online (Sandbox Code Playgroud)
我的视图看起来像:
class AppointmentViewSet(viewsets.ModelViewSet):
queryset = Appointment.objects.all()
serializer_class = AppointmentSerializer
Run Code Online (Sandbox Code Playgroud)
在我的网址中:
router.register(r'appointments', AppointmentViewSet)
Run Code Online (Sandbox Code Playgroud)
现在我想通过一些患者 ID 过滤约会列表。这个 id 应该由请求者通过 url 给出。我正在考虑使用kwargs来捕捉它。但我不知道该怎么做。我知道我必须覆盖list方法。
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?
Run Code Online (Sandbox Code Playgroud)
如何自定义URL和/或视图以适应患者 ID 参数?我只想修改列表请求,所有其他操作(创建、详细信息、销毁)都应由模型视图集的默认行为处理。
谢谢。
尝试
if(self.request.get('pid')):
pk = self.request.get('pid')
all_appointment = Appointment.objects.filter(patient__pk=pk)
Run Code Online (Sandbox Code Playgroud)
对于患者 url 将是appoinment/?pid=9
以及预约详情appoinment/9
| 归档时间: |
|
| 查看次数: |
3830 次 |
| 最近记录: |