I store a server response through a Y.io command with an on success that looks like this:
on: {
success: function (requestId, response){
sess_id = parseInt(response.responseText);
buildtable();
}
}
buildtable() creates simulated data and uses the sess_id variable to fill in a column labeled session. This way every simulated data reading is associated with a session. my models for these two tables look like this:
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)
when I try to store the simulated data into the meterdata table I get this error:
Cannot assign "5.0": "meterdata.session" must be a "session" instance
I have tried inserting it as a string, int, and float. I also am sure that there is actually a session created in the session table. How do i ensure the code understands that this number is referring to one of the ids in the session table?
edit: Here is the view that is being called when trying to save
@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)
Let me guess the value you put is the session id that's why the system telling you Cannot assign "5.0": "meterdata.session" must be a "session" instance
. If this problem occur, telling about the instance
, just add id
in the variable to make it equal in your value like this session_id
.
Other solution, so that you will not get that instance
error, don't use the id.
Ex.
<select>
{% for session in sessions %}
<option value="{{ session }}">{{ session }}</option>
{% endfor %}
</select>
As you noticed, I didn't use the session.id instead I use session only
. This is the difference between instance and not instance.