在djangorestframework中调度2分钟后的post请求?

4nkexdtk  于 2023-03-24  发布在  Go
关注(0)|答案(1)|浏览(100)

你好开发人员我坚持通过使用APScheduler在djangoRestFramework自动发布请求每2分钟请帮助做它
调度作业中的主要问题是传递请求。

class LogisticsTruckListView(APIView):
def post(self, request):
    truck_id = request.data.get('truckListId')
    queryset = LogisticsTruckList.objects.filter(truckListId=truck_id)
    serializer = LogisticsTruckListSerializer(queryset, many=True)
    data = serializer.data
    # get vehicle no
    vehicle_no = data[0]['vehicle_no']
    if vehicle_no:
        url = "https://track.cxipl.com/api/v2/phone-tracking/doc-latest-location?vehicleNumber="+vehicle_no
        headers = {
            "Content-type": "application/json",
            "authkey": "UB1CNQLKU32LPCFQJ2NGL7YY1E51HYF6"
        }
        response = requests.get(url, headers=headers, verify=False)
        if response.status_code == 200:
            data = response.json()
            return Response(data, status=status.HTTP_200_OK)
        else:
            return Response(status=status.HTTP_400_BAD_REQUEST)
    else:
        return Response(status=status.HTTP_400_BAD_REQUEST)

这是我的调度作业代码。

def start():
scheduler = BackgroundScheduler()
viewSets = LogisticsTruckListViewSet()
scheduler.add_job(viewSets.getTrackingDetails(), 'interval', minutes=1,
                  id='tracking_details__001', replace_existing=True)
scheduler.start()

尽快帮我解决
我想在Django REST框架中安排一个任务

e5nqia27

e5nqia271#

您可以使用其他库来调度任务,例如celery-beat

相关问题