我得到“详细信息”:“未找到。” 当我访问此链接 localhost:8000/portal/finalyears/1/members/2 但当我使用以下链接时,它会显示详细信息: localhost:8000/portal/finalyears/1/members/1
我的 urls.py 文件包含:
path('finalyears/<int:pk>/members/<int:id>', MemberDetailView.as_view(), name='Members_Detail'),
模型“成员”如下所示:
class Member(models.Model):
finalyear = models.ForeignKey(to=FYP, on_delete=models.CASCADE)
Student_Id = models.ForeignKey(to=Student, on_delete=models.CASCADE)
Leader_FYP = models.BooleanField(default=False)
class Meta:
verbose_name_plural = 'Members'
def __str__(self):
return '{}'.format(self.finalyear)
API.py 包含:
class MemberDetailView(generics.RetrieveUpdateDestroyAPIView):
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
# lookup_field = "member_pk"
def get_queryset(self):
fyp = self.kwargs['pk']
member = self.kwargs['id']
return Member.objects.filter(finalyear=fyp, id=member)
serializer_class = MemberSerializer
我可以通过以下代码打印数据
from PortalAPI.serializers import MemberSerializer
from PortalAPI.models import Member
for a in Member.objects.filter(finalyear=1, id=2):
... MemberSerializer(a).data