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
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.
Use APIView Realization
Use decorators api_view Realization
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 ?