簡體   English   中英

Google NDB問題,添加了密鑰屬性

[英]Google NDB Issue with adding keyproperty

我正在制作連接到移動應用程序的后端,允許用戶輸入上傳他們的位置。 我一切正常,但是我嘗試添加帳戶功能,並且不斷收到以下錯誤。

      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

當我運行以下curl命令時。 我知道帳戶密鑰在創建時是有效的。

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

想法是,對於每個位置,我還可以包括帳戶密鑰,這樣我就可以拉起僅具有特定帳戶密鑰的簽到。 這是我的班級防御

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()

這是我的郵政編碼。

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

就像錯誤所說的一樣,當您設置new_loc.account時,它期待一個鍵,但是您試圖分配一個整數。 您需要一個實際的ndb密鑰。

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

(並且請在發布之前清理代碼。發布注釋行沒有意義,外加解釋它們為何被注釋的解釋;只需將其刪除即可。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM