django rest framework list query customize json array result response because of date formatting
Use serializers
of django rest framework, create a serializer class
from rest_framework import serializers
class EmployeeSerializer(serializers.ModelSerializer):
created_at = serializers.DateTimeField(format='%Y-%m-%d %H:%M')
class Meta:
model = Employee
fields = ("id", "username", "first_name", "last_name", "created_at")
Now parse your employees queryset using serializer class.
@api_view(['POST'])
def employee_get_list_by_page(request):
employees = Employee.objects.all().values(*val_params).order_by('id')
serializer = EmployeeSerializer(employees, many=True)
# rest of your code
...
return Response(serializer.data, status=status.HTTP_200_OK)
Format strings may either be Python strftime formats which explicitly specify the format, or the special string
iso-8601
, which indicates thatISO 8601
style datetimes should be used. (eg2013-01-29T12:34:56.000000Z
)