簡體   English   中英

Django Restful API應用

[英]Django Restful API App

我制作了一個新的API應用程序來滿足我的Web應用程序的所有API請求。 因此,我想先做一些簡單的事情,然后以JSON的形式返回一個DataFrame對象。 我也在使用Django Rest Framework Library。

我的urls.py

from django.conf.urls import url, include
from rest_framework import routers
from api import views

router = routers.SimpleRouter()
router.register(r'test', views.UserViewSet)

urlpatterns = [
    url(r'^', include(router.urls))
]

我的views.py:

class UserViewSet(APIView):
    renderer_classes = (JSONRenderer, )

    def get(self):
        queryset = NAV.objects.filter(fund__account_class=0, transmission=3).values('valuation_period_end_date').annotate(
        total_nav=Sum(F('outstanding_shares_par') * F('nav'))).order_by('valuation_period_end_date')
        df = read_frame(queryset, coerce_float=True)
        df.loc[:, 'valuation_period_end_date'] = pd.to_datetime(df.valuation_period_end_date)
        df.loc[:, 'timestamp'] = df.valuation_period_end_date.astype(np.int64) // 10 ** 6
        df.loc[:, 'total_nav'] = df.total_nav
        return JsonResponse(df)

但是我收到錯誤AssertionError: base_name argument not specified, and could not automatically determine the name from the viewset, as it does not have a .queryset attribute. argument not specified, and could not automatically determine the name from the viewset, as it does not have a attribute. 我是Django Restful API Framework的新手,想知道我是否做對了?

在路由器上注冊視圖時,您似乎需要指定基本名稱。 嘗試執行此操作(將'User'更改為您要查詢的模型的名稱:

router.register(r'test', views.UserViewSet, 'User')

有關此的更多信息,請參見此處: http : //www.django-rest-framework.org/api-guide/routers/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM