2010-10-30 52 views
0

這裏還有一些關於AttributeErrors的其他問題,但我已經讀過它們,但仍不確定是什麼導致了類型不匹配在我的具體情況。AppEngine - >「AttributeError:'unicode'對象在使用blobstore時沒有屬性'has_key'」

在此先感謝您的任何想法。

我的模型:

class Object(db.Model): 
    notes = db.StringProperty(multiline=False) 
    other_item = db.ReferenceProperty(Other) 
    time = db.DateTimeProperty(auto_now_add=True) 
    new_files = blobstore.BlobReferenceProperty(required=True) 
    email = db.EmailProperty() 
    is_purple = db.BooleanProperty() 

我BlobstoreUploadHandler:

class FormUploadHandler(blobstore_handlers.BlobstoreUploadHandler): 
    def post(self): 
    try: 
     note = self.request.get('notes') 
     email_addr = self.request.get('email') 
     o = self.request.get('other') 
     upload_file = self.get_uploads()[0] 

     # Save the object record 
     new_object = Object(notes=note, 
      other=o, 
      email=email_addr, 
      is_purple=False, 
      new_files=upload_file.key()) 

     db.put(new_object) 

     # Redirect to let user know everything's peachy. 
     self.redirect('/upload_success.html') 

    except: 
     self.redirect('/upload_failure.html') 

每次我提交上傳文件的形式時,它拋出以下異常:

ERROR 2010-10-30 21:31:01,045 __init__.py:391] 'unicode' object has no attribute 'has_key' 
Traceback (most recent call last): 
    File "/home/user/Public/dir/google_appengine/google/appengine/ext/webapp/__init__.py", line 513, in __call__ 
    handler.post(*groups) 
    File "/home/user/Public/dir/myapp/myapp.py", line 187, in post 
    new_files=upload_file.key()) 
    File "/home/user/Public/dir/google_appengine/google/appengine/ext/db/__init__.py", line 813, in __init__ 
    prop.__set__(self, value) 
    File "/home/user/Public/dir/google_appengine/google/appengine/ext/db/__init__.py", line 3216, in __set__ 
    value = self.validate(value) 
    File "/home/user/Public/dir/google_appengine/google/appengine/ext/db/__init__.py", line 3246, in validate 
    if value is not None and not value.has_key(): 
AttributeError: 'unicode' object has no attribute 'has_key' 

最讓我感到困惑的是,這段代碼幾乎是直接從文檔中提取出來的,並且還有其他一些blob上傳處理程序的示例,我也在線上在教程中找到它們。

我已經運行--clear-datastore來確保我對數據庫模式所做的任何更改都不會導致問題,並且嘗試將各種東西鑄造成upload_file以查看它是否會安撫Python - 關於我搞砸了什麼想法?


編輯:我找到了一個解決方法,但它不是最理想的。

改變UploadHandler這不是解決問題:

... 
    # Save the object record 
    new_object = Object() 

    new_object.notes = note 
    new_object.other = o 
    new_object.email = email.addr 
    new_object.is_purple = False 
    new_object.new_files = upload_file.key() 

    db.put(new_object) 
    ... 

我注意到,註釋掉文件扔行同樣的問題爲other線,等後作出此開關。然而,這不是一個最佳的解決方案,因爲我無法以這種方式強制驗證(在模型中,如果根據需要設置了任何內容,我不能在沒有拋出異常的情況下聲明像上面那樣的空實體)。

有關爲什麼我無法同時聲明實體並填充它的任何想法?

回答

0

[警告講師:我知道「google_app_engine」齊爾希]

該消息表示它期待一個dict(唯一已知對象具有has_key屬性)或一個工作相似對象,而不是unicode您提供的對象。也許你應該通過upload_file,而不是upload_file.key() ...

+0

感謝您的快速響應,約翰。我試過'upload_file',但它也不起作用。另外,這似乎是它在文檔中列出的方式:http://code.google.com/appengine/docs/python/tools/webapp/blobstorehandlers.html – Andrew 2010-10-31 01:05:33

+0

實際上,'db.Model'定義了一個'has_key '方法。一個不幸的,但現在不可逆轉的命名巧合。 – 2011-12-15 00:23:05

0

has_key錯誤是由於ReferenceProperty other_items。當appengine的api期待一個字典時,你很可能會傳遞給other_items。爲了解決這個問題,你需要將other_items轉換爲散列。

+0

這個API不期望在這裏。 – 2011-12-15 00:23:17

2

您的o值爲other_item(在您的示例代碼中,您將其稱爲other,但我認爲這是一個錯字)。 o是從請求中提取的字符串,但模型定義指定它是ReferenceProperty,因此它應該是Other類的實例或db.Key對象。

如果o應該是一個字符串化的鍵,那麼請傳入db.Key(o)來反序列化它。

Object順便說一句,對於數據存儲類(或任何類,真的)來說真的很糟糕 - Python基礎對象叫做object,這只是一個大寫字母 - 很容易被誤認。

相關問題