DRF,将检索功能路由到发布功能

问题描述 投票:0回答:1

我有一个Django rest框架API,该API可以获取POST请求并在视图中具有检索方法。

我希望当用户按下发布按钮时,它将把URL路由到在视图类的检索方法中创建的渲染。

代码:

#views.py


class LocationInfoViewSet(ModelViewSet):
    # Order all objects by id, reversed.
    queryset = LocationInfo.objects.all().order_by('-id')
    serializer_class = LocationInfoSerializer

    def retrieve(self, request, *args, **kwargs):
        """
        This method is used to get the last object created by the user and render a map associated with the
        mission's name.
        """

        data = self.queryset[0]
        serialized_data = LocationInfoSerializer(data, many=False)
        points = list(serialized_data.data.values())

        assign_gdt1 = GeoPoint(lat=points[2], long=points[3])
        assign_gdt2 = GeoPoint(lat=points[4], long=points[5])
        assign_uav = GeoPoint(lat=points[6], long=points[7], elevation=points[-3])

        # Geo locations from the POST request.
        gdt1 = [assign_gdt1.get_lat(), assign_gdt1.get_long()]
        gdt2 = [assign_gdt2.get_lat(), assign_gdt2.get_long()]
        uav = [assign_uav.get_lat(), assign_uav.get_long(), assign_uav.get_elevation()]

        mission_name = points[1]

        try:
            # Check if a file already exists in the DB.
            HTMLFileInteractionWithDB.table = THREE_POINTS_TRINAGULATION
            openfile = HTMLFileInteractionWithDB.return_file_from_db(mission_name=mission_name)
            return render(request, openfile)
        except:
            # Create a new file if one does not exists.
            # The main function Creates an HTML File to be rendered.
            return render(request, main(gdt1, gdt2, uav,
                                        gdt1_elev=assign_gdt1.get_elevation(),
                                        gdt2_elev=assign_gdt2.get_elevation(),
                                        mission_name=mission_name
                                        )
                          )

任务名称是主键,因此,要访问检索方法,用户需要转到URL行并编写任务名称以使方法起作用。

因此,如何在我的项目中(URL,视图...)在哪里创建此路线。

示例:

example

python django-rest-framework routing
1个回答
0
投票

我对这种观点的目的有点困惑。

retrieve方法从查询集列表中检索特定对象时正确使用。 IE,您的位置信息之一。它总是一个获取请求。

如果我是你,我将为此创建一个完全独立的视图或视图集方法/操作。

而不是为此使用retrieve,我们可以创建一个全新的方法:

from rest_framework.decorators import action

class LocationInfoViewSet(ModelViewSet):
    # Order all objects by id, reversed.
    queryset = LocationInfo.objects.all().order_by('-id')
    serializer_class = LocationInfoSerializer

    # {The rest of your methods, etc...}

    @action(detail=False, methods=["post"])
    def last_object_by_user(self, request, *args, **kwargs):
        # your query to get the last object by your user
        queryset = LocationInfo.objects.filter(created_by=request.user).latest()
        # The rest of your code

您还注意到我对您的查询集做了一些修改。您当前的查询集为我们提供了任何用户创建的最后一个LocationInfo。您是否在LocationInfo中创建了一个字段来跟踪谁创建了该LocationInfo?]

这里是文档:Marking extra actions for routing

注意:我没有测试此代码,因此如果您将其复制粘贴,则可能无法正常工作,但是这个想法很重要。

© www.soinside.com 2019 - 2024. All rights reserved.