[英]How can i get the id of each record in ListApiView in DRF
I am facing an issue while getting the promary_ket(id) of record ListApiView in the Django rest framework. 我在Django rest框架中获取记录ListApiView的promary_ket(id)时遇到问题。 I am also implementing the Custom pagination.
我也在实施自定义分页。
Here is my serializer: 这是我的序列化器:
class AllTestListSerializer(serializers.ModelSerializer):
strip_img = serializers.FileField(required=False)
cropped_img = serializers.FileField(required=False)
class Meta:
model = TakeTestDetails
fields = ['strip_img',
'cropped_img',
'period_cd',
'date',
'time',
'test_type',
'test_result',
'cycle_id',
'raw_data']
Here is my ListApiView Code: 这是我的ListApiView代码:
class TestList(ListAPIView):
permission_classes = (IsAuthenticated, )
serializer_class = AllTestListSerializer
pagination_class = CustomPagination
def get_queryset(self):
return TakeTestDetails.objects.filter(user=self.request.user).order_by('date', 'time').all()
And here is my CustomPagination code: 这是我的CustomPagination代码:
class CustomPagination(pagination.PageNumberPagination):
def get_paginated_response(self, data):
response_data = {
'status': '200',
'message': 'Success',
'count': self.page.paginator.count,
'next': self.get_next_link(),
'previous': self.get_previous_link(),
}
if data:
new_cycle_data = []
for row in data:
row_dict = dict(row)
print(row)
cycle_id = row_dict['cycle_id']
cycle = TestCycle.objects.get(id=cycle_id)
cycle_no = cycle.cycle_no
cycle_start_date = cycle.cycle_start_date
cycle_end_date = cycle.cycle_end_date
cycle_start_date = change_date_format(cycle_start_date)
cycle_end_date = change_date_format(cycle_end_date)
test_date = change_date_format(row_dict['date'])
row_dict['date'] = test_date
row_dict['cycle_no'] = cycle_no
row_dict['cycle_start_date'] = cycle_start_date
row_dict['cycle_end_date'] = cycle_end_date
new_cycle_data.append(row_dict)
response_data['data'] = new_cycle_data
else:
return Response({
'status': '404',
'message': "You haven't performed any test yet",
'count': self.page.paginator.count,
'next': self.get_next_link(),
'previous': self.get_previous_link(),
'data': data
})
return Response(response_data)
Now what i want is also to get the primary_key/id of each row/record. 现在我想要的也是获取每行/记录的primary_key / id。 But it's not giving me the id in data.
但这并没有给我数据中的ID。 How can i get the id?
我如何获得身份证?
Thanks in advance 提前致谢
add ID to the serializer 向序列化器添加ID
class AllTestListSerializer(serializers.ModelSerializer):
strip_img = serializers.FileField(required=False)
cropped_img = serializers.FileField(required=False)
class Meta:
model = TakeTestDetails
fields = ['id'
'strip_img',
'cropped_img',
'period_cd',
'date',
'time',
'test_type',
'test_result',
'cycle_id',
'raw_data']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.