程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django rest_ API for framework custom content

編輯:Python

Abstract

In knowing and knowing django rest_framwork And I haven't known each other for decades , Just want to customize your own interface content , But I see examples and examples from netizens , Only about the model viewset, It's mainly written by myself. It doesn't work. I have read the document given several times , It doesn't seem to say how the interface outside the model operates , There are no functions to cover .

Problem core ,Djangorestframwork Build model independent  API |Django rest framwork without model | Django rest framwork without query_set

 

Environmental Science

Django==3.1.7

djangorestframework==3.12.4

Relevant questions and contents

  • router.register in basename What's the usage?

  • Defining interfaces ping Custom response content pong

  • What problems have arisen

    • should either include a `queryset` attribute, or override the `get_queryset()` method.

    • The route is registered but not effective

    • 'Ping_ViewSet' should either include a `queryset` attribute, or override the `get_queryset()` method.

Conclusion

  • rest_framwork.router.register You can only register ViewSet
  • APIview Model independent or no can be implemented queryset The direct return custom content of the model , But it can't be used. drf Of router register
  • Of the pit url The name can't be ping--> See  Django url The name can't be ping The strange phenomenon of  

Solution

Use APIView Realization

Use decorators api_view Realization

The exploration process

problem :should either include a `queryset` attribute, or override the `get_queryset()` method.

class User_Serializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('id', 'username', 'email',)
class User_ViewSet(viewsets.ReadOnlyModelViewSet):
queryset = User.objects.all()
serializer_class = User_Serializer
#######here mine
class Ping_ViewSet(viewsets.GenericViewSet):
'''
ping<---->
'''
pass
def get(self,request,*k,**kw):
pass
def post(self,request,*k,**kw):
pass
router = DefaultRouter()
router.register(r'users', User_ViewSet)
router.register(r'ping', Ping_ViewSet)
urlpatterns = [
path('', include(router.urls)),
]

Question why : In my Ping_ViewSet Not defined in query_set attribute , Maybe there is no data representation for the parent class of the model , You need to be in register Add... To add basename Parameters

'basename' argument not specified, and could not automatically determine the name from the viewset, as it does not have a '.queryset' attribute.

Translation is :basename Parameter is not specified , And can't automatically determine viewset My name comes from , Because it doesn't have  '.queryset' attribute

source :https://www.django-rest-framework.org/api-guide/routers/

  Okay, plus basename, Imitate others basename, I don't know what the value should be

router.register(r'ping', plum.Ping_ViewSet,basename='ping')

It's really not wrong , But I can't visit my ping Interface , stay APIroot( visit your_server:port) You can't even see the shadow in the ,quickstart But you can see , So the route is registered but not effective ?

 

 

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved