0

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)
Khamey
  • 481
  • 4
  • 17
  • That error is coming from somewhere in your Django code, but you don't show the relevant part. – Daniel Roseman Jul 05 '13 at 15:34
  • @DanielRoseman I'm not really sure what you want me to show. I see the error occurs somewher in the io-base.js, but if I change my session column in my meterdata table from ForeignKey() to FloatField() everything is fine. I want the tables to be linked though. – Khamey Jul 05 '13 at 15:41
  • OK, thank you for posting the view code. I still don't understand though: as the error implies, `data['session']` appears to be "5.0". How does that relate to the Session table which has `start_time` and `end_time`? Is it supposed to be the PK of that table? – Daniel Roseman Jul 05 '13 at 15:44
  • yes when you create a model in django, from what I understand if you do not create a primary key column it will automatically do it for you. so I have a view that creates this session with a start time and automatically numbers it depending what is currently in the table. the id(pk) is automatically created and sent back in the server response which is then saved and used to generate the data. – Khamey Jul 05 '13 at 15:47
  • 2
    try to put id extension in your session like this `session_id = data['session']` and foreignkey id must be an `int` – catherine Jul 05 '13 at 15:52
  • @catherine I don't know what I did. I deleted the database and tried again. It works now. session_id int(data['session']) fixed it. – Khamey Jul 05 '13 at 20:30
  • @Khamey do I have to post my answer? – catherine Jul 06 '13 at 06:01
  • @catherine if you want to. I would gladly upvote it and select it as the accepted solution. – Khamey Jul 08 '13 at 17:17

1 Answers1

1

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.

catherine
  • 22,492
  • 12
  • 61
  • 85
  • This solution did not work at first, but if this is similar to your problem ensure your models.py file is correct, delete the entire database, then run a python manage.py syncdb again to recreate the database. Thanks for your help! – Khamey Jul 11 '13 at 14:23