我通过 Y.io 命令存储服务器响应,成功后如下所示:
on: {
success: function (requestId, response){
sess_id = parseInt(response.responseText);
buildtable();
}
}
buildtable() 创建模拟数据并使用 sess_id 变量填充标记为 session 的列。这样,每个模拟数据读取都与一个会话相关联。我对这两个表的模型如下所示:
class session(models.Model):
start_time = models.IntegerField()
end_time = models.IntegerField(null=True)
def __unicode__(self):
return unicode(self.pk)
class meterdata(models.Model):
time_elapsed = models.FloatField()
volts = models.FloatField()
amps = models.FloatField()
kW = models.FloatField()
kWh = models.FloatField()
session = models.ForeignKey(session)
def __unicode__(self):
return unicode(self.session)
当我尝试将模拟数据存储到meterdata表中时,出现此错误:
Cannot assign "5.0": "meterdata.session" must be a "session" instance
我尝试将它作为字符串、int 和浮点数插入。我也确信会话表中实际上创建了一个会话。我如何确保代码理解这个数字是指会话表中的一个 ID?
编辑:这是尝试保存时调用的视图
@csrf_exempt
def save(request):
if request.method == 'POST':
rawdata1 = request.body
rawdata2 = json.loads(rawdata1)
for data in rawdata2:
x = meterdata(time_elapsed = data['time_elapsed'], volts = data['volts'], amps = data['amps'], kW = data['kW'], kWh = data['kWh'], session = data['session'])
x.save()
return HttpResponse(rawdata2)