Def does not work in django

I have defined a class with a get method that will be executed when the user sends a request using the get method (mainly to extract the data from the data and then make some modifications before sending it to the front end). But I gave it a try. When a user initiates a get request, it only executes

.
queryset = Product.objects.all()
serializer_class = ProductSerializer

none of the methods of get have been executed. May I ask what"s going on

?
-sharp-sharp urls.py
router.register(r"getList", ProductListViewset)
-sharp-sharp views.py

class ProductListViewset(viewsets.ModelViewSet):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer

    def get(self, request, format=None):
        serializer = ProductSerializer
        data = serializer.data
        username = data.get("user_name")
        user = User.objects.get(username__exact=username)
        new_data = {
            "id": data.get("id"),
            "user_name": data.get("user_name"),
            "user_image_URL": user.get("user_image_URL"),
            "c_time": data.get("c_time"),
            "goods_price": data.get("goods_price"),
            "title": data.get("title"),
            "description": data.get("description")
        }
        return Response(new_data, 200)

you need to take a closer look at the restfulframework framework document.

class ModelViewSet(mixins.CreateModelMixin,
                   mixins.RetrieveModelMixin,
                   mixins.UpdateModelMixin,
                   mixins.DestroyModelMixin,
                   mixins.ListModelMixin,
                   GenericViewSet):
    """
    A viewset that provides default `create()`, `retrieve()`, `update()`,
    `partial_update()`, `destroy()` and `list()` actions.
    """
    pass

the matching of action corresponding to various http request methods has been encapsulated in modelviewset.
, and the specific matching rules are defined in the SimpleRouter class of the routers module

.
class SimpleRouter(BaseRouter):

    routes = [
        -sharp List route.
        Route(
            url=r'^{prefix}{trailing_slash}$',
            mapping={
                'get': 'list',
                'post': 'create'
            },
            name='{basename}-list',
            detail=False,
            initkwargs={'suffix': 'List'}
        ),
        -sharp Dynamically generated list routes. Generated using
        -sharp @action(detail=False) decorator on methods of the viewset.
        DynamicRoute(
            url=r'^{prefix}/{url_path}{trailing_slash}$',
            name='{basename}-{url_name}',
            detail=False,
            initkwargs={}
        ),
        -sharp Detail route.
        Route(
            url=r'^{prefix}/{lookup}{trailing_slash}$',
            mapping={
                'get': 'retrieve',
                'put': 'update',
                'patch': 'partial_update',
                'delete': 'destroy'
            },
            name='{basename}-detail',
            detail=True,
            initkwargs={'suffix': 'Instance'}
        ),
        -sharp Dynamically generated detail routes. Generated using
        -sharp @action(detail=True) decorator on methods of the viewset.
        DynamicRoute(
            url=r'^{prefix}/{lookup}/{url_path}{trailing_slash}$',
            name='{basename}-{url_name}',
            detail=True,
            initkwargs={}
        ),
    ]

so back to your question, in fact, the framework already provides routing and action, by default. What you need to do is to override the methods in mixin to implement customization (that is, your own business logic)

router = SimpleRouter()
router.register('product', ProductViewset)
-sharp ProductViewsetrestful
-sharp viewset def list()
-sharp ListModelMixinlist

class ListModelMixin(object):
    """
    List a queryset.
    """
    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())

        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)

in addition, from the code above,
I don't think you understand restfulframework.
I suggest you follow the examples on the official website, learn step by step, and raise questions in the learning process. Let's look at them together.
ide/routers/" rel=" nofollow noreferrer "> https://www.django-rest-frame.

Menu