我的function应用程序中有一个函数,它向Django API发送数据。一旦接收到这些数据,django就会调用一个外部python函数来执行一些代码。
目前,我有javascript,当它收到一个ok
响应时,给我一个警告。但是,Django在外部函数完成之前不会发送此响应;这是一个问题,因为外部函数可能需要一个小时才能根据用户的输入运行。在外部python代码开始成功运行后,是否可以将其更改为发出一个警告,并在函数完成时再次发出警报?
我理解它们在将数据发送到API时可能是失败的,API无法访问数据可能是因为数据类型不匹配,最后是如果数据与外部函数不兼容。我正在寻找来自异步函数React的3种不同的响应。
export const SendData = (url, props) =>{ //this url is the url to the DataInput api view
const data1 = document.getElementById('data1')
const data2 = document.getElementById('data2')
async function postData() {
var res = ''
const options ={
method : 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8'
},
body: JSON.stringify({
data_one: data1,
data_two: data2
})
}
const response = await fetch(url, options)
.then(response => {
if (response.ok) {
alert("Data Sent!")
}
else {
alert("An error has occurred.\nWere all fields filled out?")
}
});
}
postData()
};
models.py
class DataInput(models.Model):
data_one = models.IntegerField(
max_length=30,
default=5)
data_two = models.IntegerField(
max_length=30,
default=4)
class OtherData(models.Model):
other_data = models.IntegerField(
max_length=5,
default=10)
@receiver(post_save, sender=DataInput, dispatch_uid="extra function")
def extra_function(sender, instance, created, *args, **kwargs):
#dummy function to show reliance on data
for i in OtherData[0].other_data:
print(instance.data_two + instance.data_one)
serializer.py
from rest_framework import serializers
from .models import DataInput
from .models import OtherData
class DataSerializer(serilizers.ModelSerializer):
class Meta:
model = DataInput
fields = ('data_one', 'data_two')
class OtherDataSerializer(serializer.ModelSerializer):
class Meta:
model = OtherData
fields = ('other_data')
发布于 2020-09-16 16:23:29
https://stackoverflow.com/questions/63929084
复制