如何让视图知道另一个视图中的模型实例何时被保存

问题描述

我正在使用一个不直接发送响应的 API(也许这是一个 api 标准)。一个粗略的例子:

def callbackview(request):
    #This view will receive the actual required data from the api server,and save response to 
    #the db(there might be a better way to do this)
    
    # Actually uses drf to parse/save the data
    ...
    return Response(...)
    
def apisenderview(request):
    #send the api request
    resp=requests.get("https://api_url.com/api",{"callbackurl":"callbackview",...})
    ...

问题在于 resp 中的 apisenderview 收到的响应只是服务器队列详细信息。我认为我需要的是一种让 apisenderview 知道 API 服务器何时向 callbackview 发送响应的方法。我正在考虑的解决方案是:

...
def apisenderview(request):
    #send the api request
    resp=requests.get("https://api_url.com/api",{"callbackurl":"callbackview"})
    callbackinstance=None:
    while not callbackinstance:
        callbackqs=callbackmodel.objects.filter(queue_id=resp.queue_id)
        if callbackqs.exists():
            callbackinstance=callbackqs.first()
    
    #Continue with view operations
    ...

但是,上面的解决方案可能有太多的数据库调用(我还没有测试过)。我如何优化这个过程以确保最小的apisenderview响应时间?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)