Django休息框架,结果不在retrieveapiview中返回

时间:2018-04-16 18:03:20

标签: django django-rest-framework

我是django休息框架的新手。我有2个型号,Store和Rate。对于此API,您输入商店编号以查找状态。并找到该州的费率。我能够获得速率并在控制台中打印。但由于某些原因,我一直没有找到。

class RateDetailAPIView(RetrieveAPIView):
    def get_queryset(self):
        storeNumber = str(self.kwargs['store_number'])
        store = Store.objects.get(store_number = storeNumber)
        rate = Rate.objects.get(state = store.state)

        print(rate.proposed_per_store_rate)
        return rate
    #queryset = self.get_queryset()
    lookup_field = 'store_number'
    serializer_class = RateSerializer

这是我在控制台中得到的。 1700是率。然后我找不到。

1700
Not Found: /api/rates/1910003/
[16/Apr/2018 17:56:07] "GET /api/rates/1910003/ HTTP/1.1" 404 5249

型号:

from django.db import models

class Store(models.Model):
    store_number = models.CharField(max_length=50, blank=False, unique=True)
    address = models.CharField(max_length=255, blank=False, unique=False)
    address2 = models.CharField(max_length=255, blank=True, unique=False)
    city = models.CharField(max_length=50, blank=False, unique=False)
    state = models.CharField(max_length=10, blank=False, unique=False)
    zip_code = models.CharField(max_length=50, blank=True, unique=False)
    phone_number = models.CharField(max_length=50, blank=True, unique=False)
    store_hours = models.CharField(max_length=255, blank=True, unique=False)
    latitude = models.CharField(max_length=50, blank=True, unique=False)
    longitude = models.CharField(max_length=50, blank=True, unique=False)
    geo_accuracy = models.CharField(max_length=255, blank=True, unique=False)
    country = models.CharField(max_length=50, blank=True, unique=False)
    country_code = models.CharField(max_length=10, blank=True, unique=False)
    county = models.CharField(max_length=50, blank=True, unique=False)

    def __str__(self):
        return"{}".format(self.store_number)

class Rate(models.Model):
    number_of_stores = models.IntegerField()
    state = models.CharField(max_length=5, blank=False, unique=False)
    proposed_per_store_rate = models.IntegerField()
    proposed_prem = models.IntegerField()

    def __str__(self):
        return "{}".format(self.state + ":" + str(self.proposed_per_store_rate))

串行器:

from rest_framework import serializers
from .models import Store, StoreRate, Rate

class StoreSerializer(serializers.ModelSerializer):

    class Meta:
        model = Store
        fields = ('id', "store_number", "address", "address2", "city", "state", "zip_code", "phone_number", "store_hours", "latitude", "longitude", "geo_accuracy", "country", "country_code", "county")



class RateSerializer(serializers.ModelSerializer):

    class Meta:
        model = Rate
        fields = ('id', "number_of_stores", "state", "proposed_per_store_rate", "proposed_prem")

URL:

from django.conf.urls import url, include
from rest_framework.urlpatterns import format_suffix_patterns
from .views import (AllStoresAPIView, StoreDetailAPIView, StoreLoadAPIView, StoreRateDetailAPIView, RateDetailAPIView)

urlpatterns = {
    url(r'^api/stores/$', AllStoresAPIView.as_view(), name="stores"),
    #url(r'^api/stores/load/$', StoreLoadAPIView.as_view(), name="load"),
    url(r'^api/stores/(?P<store_number>\d+)/$', StoreDetailAPIView.as_view(), name='detail'),
    #url(r'^api/quote/(?P<store_number>\d+)/$', StoreRateDetailAPIView.as_view(), name='quote'),
    url(r'^api/rates/(?P<store_number>\d+)/$', RateDetailAPIView.as_view(), name='rate'),
}

urlpatterns = format_suffix_patterns(urlpatterns)

1 个答案:

答案 0 :(得分:1)

get_queryset应该返回queryset而不是实例,因为你的情况RetrieveAPIView需要覆盖get_object方法,例如:

class RateDetailAPIView(RetrieveAPIView):
    queryset = Rate.objects.all()
    lookup_field = 'store_number'
    serializer_class = RateSerializer

    def get_object(self):
    #   ^^^^^^^^^
        storeNumber = str(self.kwargs['store_number'])
        store = Store.objects.get(store_number = storeNumber)
        rate = Rate.objects.get(state = store.state)

        print(rate.proposed_per_store_rate)
        return rate

您可以阅读更多get_object