user3334886
user3334886

Reputation: 11

Google NDB Issue with adding keyproperty

I am making the backend that will connect to a mobile app that allows the user to enter upload their location. I had everything working however I am trying to add an accounts feature and I keep getting the following error.

      File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\model.py", line 2011, in _validate
raise datastore_errors.BadValueError('Expected Key, got %r' % (value,))
BadValueError: Expected Key, got 5733953138851840L

When I run the following curl command. I know the account key is valid as i just created it.

curl -X POST -H "Content-Type: application/json" -d '{"time":"Test Account", "longitude": 11.11, "lattitude": 22.22, "account": 5733953138851840}' http://localhost:14080/loc      

The idea is that for each location I can also include the account key so that I can pull up the checkins that only have a certain account key. here is my class defs

class Loc(ndb.Model):
    time = ndb.StringProperty()
    longitude = ndb.FloatProperty()
    lattitude = ndb.FloatProperty()
    account = ndb.KeyProperty(kind="Account")


class Account(ndb.Model):
    name = ndb.StringProperty()
    password = ndb.StringProperty()

Here is my post code.

def post(self):
    uinput= self.request.body
    r=uinput
    j = json.loads(r)
    time=j['time']
    longitude=j['longitude']
    lattitude=j['lattitude']
    account=j['account']
    if time:
        new_loc = class_def.Loc()
        new_loc.time=time
        if longitude:
            try:
                new_loc.longitude= float(longitude)
            except ValueError:
                print("That's not an int!")
        if lattitude:
            try:
                new_loc.lattitude= float(lattitude)
            except ValueError:
                print("That's not an int!")
        if account:
            new_loc.account=account
        key = new_loc.put()
        out= new_loc.to_dict()
        self.response.write(json.dumps(out))

    else:
        #self.response.status = 400
        return

Upvotes: 0

Views: 203

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600041

Like the error says, it's expecting a key when you set new_loc.account, but you're trying to assign an integer. You need an actual ndb key.

new_loc.account = ndb.Key('Account', account)

(And please clean up your code before posting. There's no point posting commented lines, plus an explanation why they're commented; just remove them.)

Upvotes: 1

Related Questions