簡體   English   中英

錯誤BadValueError:虛擬屬性是只讀的

[英]Error BadValueError: Virtual property is read-only

在遇到錯誤

BadValueError: Virtual property is read-only

我沒有在文檔中找到有關此錯誤的任何內容。 這是什么意思? 這是代碼的相關部分。 它一直有效,直到最近的SDK更新為止。

date = dt.datetime.strptime(self.json['birthday'],"%Y-%m-%d").date()
if self.json.has_key('avatar'):
    img = base64.b64decode(self.json['avatar'])
else:
    img = None
    log.info('No avatar')
user = db.User(
    name=self.json['name'], 
    password=self.json['password'], 
    full_name=self.json['full_name'],
    email = self.json['email'],
    birthday = date,
    avatar = img)
user.put()

服務器日志

  File "/api/tornado/web.py", line 1064, in _execute
    getattr(self, self.request.method.lower())(*args, **kwargs)
  File "user.py", line 31, in post
    avatar = img)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/db/__init__.py", line 970, in __init__
    prop.__set__(self, value)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/db/__init__.py", line 615, in __set__
    setattr(model_instance, self._attr_name(), value)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/db/__init__.py", line 3874, in __set__
    raise BadValueError('Virtual property is read-only')
BadValueError: Virtual property is read-only
INFO     2013-06-08 07:44:37,776 server.py:585] default: "POST /api/user/create HTTP/1.1" 500 93

數據庫模型

class User(db.Model):
    name = db.StringProperty(required=True)
    password = db.StringProperty(required=True)
    email = db.StringProperty(required=True)
    full_name = db.StringProperty()    
    birthday = db.DateProperty()
    avatar = db.BlobProperty()

如果我刪除

avatar = img

然后上面一行的錯誤。

您錯過的是文檔中的以下內容:

Blob is for binary data, such as images. **It takes a str value**, but this value
is stored as a byte string and is not encoded as text. Use a Text instance for 
large text data.

您需要做的是閱讀以下教程,以上傳Blob: https : //developers.google.com/appengine/docs/python/blobstore

並在您的模型中:

 avatar = db.BlobProperty() <-- you store the key to the uploaded avatar.

因此,在模型中,您只需將密鑰存儲到Blob引用,而不是Blob本身。 我希望這很清楚。

也可以看看下面的教程,這個教程對我有很大幫助: https : //gist.github.com/348025/d4fc873bda4a650210559f881738cc55a667ff2a

該異常來自_ReverseReferenceProperty類。 當您具有指向db.ReferenceProperty模型時,似乎會使用它。 您沒有提供足夠的詳細信息,但是我懷疑您有另一個使用db.ReferenceProperty模型,並且您將collection_name設置為與User的現有屬性之一相同的名稱。

例如,如果您有一個這樣的模型:

class Email(db.Model):
  user = db.ReferenceProperty(User, collection_name=email)

User.email將變為只讀,並且在嘗試設置它時會看到該異常。 搜索其他模型以查看是否存在db.ReferenceProperty(User, collection_name=...)

avatar = img)上的錯誤會引起誤解。 那只是多行語句的最后一行。 它可能是該聲明的任何部分。

暫無
暫無

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

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