基本上,我正在尝试检索有关汽车的特定VIN的信息,我使用URL模式:ValueError at /api/v1/purchases/1D7RV1CT0AS168723/
,其中VIN作为pk。之前一切正常,不确定发生什么变化
views.py
-*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from rest_framework import viewsets
from django.shortcuts import get_object_or_404, get_list_or_404
from .models import GetPurchases, CarFax
from .serializers import PurchasesSerializer
from .serializers import CarFaxSerializer
from rest_framework.response import Response
# Create your views here.
class getPurchases(viewsets.ModelViewSet):
''' The actions provided by the ModelViewSet class are .list(), .retrieve(),
.create(), .update(), .partial_update(), and .destroy(). '''
queryset = GetPurchases.objects.all()
serializer_class = PurchasesSerializer
def list(self, request):
# accessed at url: ^api/v1/purchases/$
queryset = GetPurchases.objects.all()
serializer = PurchasesSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
# accessed at url: ^api/v1/purchases/{pk}/$
queryset = GetPurchases.objects.all()
# https://docs.djangoproject.com/en/2.1/topics/http/shortcuts/#get-object-or-404
record = get_object_or_404(queryset, vin__exact=pk)
serializer = PurchasesSerializer(record)
return Response(serializer.data)