BadRequestError:BLOB、ENITY\u PROTO或TEXT property简明\u主题必须位于原始\u属性字段中

2024-04-27 00:19:45 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的模型的代码。你知道吗

from google.appengine.ext import ndb
from modules.admin.models.Author import Author
from modules.hod.models.Concept import Concept


class Post(ndb.Model):
    author_key = ndb.KeyProperty(kind=Author)
    content = ndb.StringProperty(indexed=False)
    created = ndb.DateTimeProperty(auto_now_add=True)
    title = ndb.StringProperty(indexed=True)
    topics = ndb.StructuredProperty(Concept, repeated=True)

    def get_important_topics(self):
        return filter(lambda x: x.occurrences > 1, self.topics)

    concise_topics = ndb.ComputedProperty(get_important_topics, repeated=True)

下面是创建模型的路径。你知道吗

@admin_home_routes.route('/SavePost', methods=['POST'])
@authenticate_admin
def save_new_post():
    post_data = request.form['newpost']
    new_post = Post(parent=posts_key())
    author = get_author_by_email(users.get_current_user().email())
    if len(author) > 0:
        new_post.author_key = author[0].key
    else:
        author = Author(parent=author_key())
        author.email = users.get_current_user().email()
        author.name = users.get_current_user().email()
        author.identity = users.get_current_user().user_id()
        key = author.put()
        new_post.author_key = key
    new_post.content = post_data
    concepts = get_topics(post_data)
    _concepts = []
    if len(concepts) > 0:
        for concept in concepts['concepts']:
            temp_concept = Concept(name=concept['concept'], occurrences=concept['occurrences'])
            _concepts.append(temp_concept)
    new_post.topics = _concepts
    new_post.put()
    return redirect('/Admin/Posts')

我得到的错误是

WARNING  2016-09-19 20:44:47,227 urlfetch_stub.py:540] Stripped prohibited headers from URLFetch request: ['Host', 'Content-Length']
WARNING  2016-09-19 10:44:48,937 tasklets.py:468] suspended generator _put_tasklet(context.py:358) raised BadRequestError(BLOB, ENITY_PROTO or TEXT property concise_topics must be in a raw_property field)
WARNING  2016-09-19 10:44:48,937 tasklets.py:468] suspended generator put(context.py:824) raised BadRequestError(BLOB, ENITY_PROTO or TEXT property concise_topics must be in a raw_property field)
ERROR    2016-09-19 10:44:48,941 app.py:1587] Exception on /Admin/SavePost [POST]
Traceback (most recent call last):
  File "C:\Code\zion-alpha\lib\flask\app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Code\zion-alpha\lib\flask\app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Code\zion-alpha\lib\flask\app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Code\zion-alpha\lib\flask\app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Code\zion-alpha\lib\flask\app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Code\zion-alpha\modules\admin\decorators\authentication.py", line 16, in authenticate_and_call
    return func(*args, **kwargs)
  File "C:\Code\zion-alpha\modules\admin\routes\admin_routes.py", line 72, in save_new_post
    new_post.put()
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\model.py", line 3451, in _put
    return self._put_async(**ctx_options).get_result()
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 383, in get_result
    self.check_success()
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 427, in _help_tasklet_along
    value = gen.throw(exc.__class__, exc, tb)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\context.py", line 824, in put
    key = yield self._put_batcher.add(entity, options)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 427, in _help_tasklet_along
    value = gen.throw(exc.__class__, exc, tb)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\context.py", line 358, in _put_tasklet
    keys = yield self._conn.async_put(options, datastore_entities)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 513, in _on_rpc_completion
    result = rpc.get_result()
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_stub_map.py", line 613, in get_result
    return self.__get_result_hook(self)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\datastore\datastore_rpc.py", line 1881, in __put_hook
    self.check_rpc_success(rpc)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\datastore\datastore_rpc.py", line 1373, in check_rpc_success
    raise _ToDatastoreError(err)
BadRequestError: BLOB, ENITY_PROTO or TEXT property concise_topics must be in a raw_property field
INFO     2016-09-19 20:44:49,036 module.py:788] default: "POST /Admin/SavePost HTTP/1.1" 500 291
INFO     2016-09-19 20:45:04,424 module.py:402] [default] Detected file changes:

Tags: inpyselfgetputgooglelinefiles
2条回答

我们可以从get_important_topics返回Concept。例如,如果Concept有字段topics,那么它可能是例如:你知道吗

def get_important_topics(self):
    return Concept(topics=filter(lambda x: x.occurrences > 1, self.topics))

我会用_pre_put_hook而不是ComputedProperty

topics = ndb.StructuredProperty(Concept, repeated=True)
concise_topics = ndb.StructuredProperty(Concept, repeated=True)

def _pre_put_hook(self):
    self.concise_topics = filter(lambda x: x.occurrences > 1, self.topics)

相关问题 更多 >