开发者

Error firing in my simple Google App Engine test

开发者 https://www.devze.com 2023-02-04 08:52 出处:网络
I\'m trying to retrieve a list of saved Message objects stored in the DataStore. I\'m getting this error:

I'm trying to retrieve a list of saved Message objects stored in the DataStore. I'm getting this error:

Traceback (most recent call last):

File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\webapp__init__.py", line 515, in call handler.get(*groups) File "C:\Users\Sergio\Downloads\python-test\python-test\gae_test\handlers.py", line 23, in get messages = data.get_messages() File "C:\Users\Sergio\Downloads\python-test\python-test\gae_test\data.py", line 12, in get_messages dbMessage = db.GqlQuery("SELECT * FROM Message ORDER BY author DESC")

File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db__init__.py", line 2296, in init model_class = class_for_kind(self._proto_query._entity) File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db__init__.py", line 266, in class_for_kind raise KindError('No implementation for kind \'%s\'' % kind) KindError: No implementation for kind 'Message'

Maybe someone with experience can explain what that exception means exactly.

Her开发者_运维技巧e's my code:

class MainHandler(BaseHandler):
  def get(self):
    messages = data.get_messages()
    return self.render('index.html', messages=messages)

#This is in data.py
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class Message(object):
  def __init__(self, body, author, email, phone):
    self.body = body
    self.author = author
    self.email = email
    self.phone = phone

def get_messages():
  #TODO: Implement
  dbMessage = db.GqlQuery("SELECT * FROM Message ORDER BY author DESC")

  messages =  []  
  for message in dbMessages:
    messages.append(message)

  return messages

  #return [
  #  Message('Hey, how are you doing?', 'Joel Hodgson', 'joel@hogson.com', '847-555-1020'),
  #  Message('Hey there! Love the website.', 'Brian Gordy', 'bg@gordoindustries.com', None),
  #  Message('What is this?', 'Linda Bleeker', None, '415-555-5555'),
  #]

#This is in Index.html:

{% extends "base.html" %}

{% block title %}Messages{% endblock %}

{% block content %}
  <h2>Leave a new message</h2>
  {% include "create.html" %}

  <h2>Existing Messages</h2>
  <div id="list">
    {% include "list.html" %}
  </div>
{% endblock %}

#And this in list.html

<ul id="messages">
  {% for message in messages %}
  <li>
    {{ message.author }}: "{{ message.body }}"
    <span class="contact-info">{{ message.phone }} &bull; {{ message.email }}</span>
  </li>
  {% endfor %}
</ul>

What am I doing wrong?

Thank you!


You're probably missing an import.

For a gql query to execute, you have to import the actual models it references.


This happens because the model class - in this case, 'Message' - hasn't been loaded when you execute the query. I don't see it anywhere in the snippet you pasted - make sure it's included, and is imported before you execute the query.


i dont see the Message Class which represents the Message's elements

insert this and if its inserted already please notify me

from google.appengine.ext import db

class Message(db.Model): author = db.StringProperty() body= db.StringProperty() phone= db.StringProperty() email = db.StringProperty()

and offcourse there should messages in the DataStore to retrieve

so you should insert messages using the put Method for example in this scenario in the post method

message = Message()
message.author = "the author"
message.body = "the body"
message.phone ="phone number"
message.email = "email"
message.put()


It seems like your Message class does not extend db.Model or ndb.Model.

Also, and this is a matter of taste, I'd prefer to have Message.query() to get all objects of type Message rather than a GQL query.

Check the test code for a library I wrote:

The model definition is at: https://github.com/rbanffy/appengine-fixture-loader/blob/master/tests/multi_level_tests.py#L15-L34

And the "SELECT * FROM Person" equivalent: https://github.com/rbanffy/appengine-fixture-loader/blob/master/tests/multi_level_tests.py#L56

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号