1

我正在使用 webapp2,ndb 模型。
我试图将 django 模型表单与 ndb 模型一起使用。以下是我的设置中的相关部分:

应用程序.yaml:

libraries:
- name: webapp2
  version: "2.5.1"
- name: jinja2
  version: latest
- name: markupsafe                                                              
  version: latest
- name: django
  version: 1.3

模型.py:

class SubTask(ndb.Model):

    task = ndb.StructuredProperty(TaskList)
    description = ndb.TextProperty()
    start_time = ndb.DateTimeProperty()
    end_time = ndb.DateTimeProperty()

表格.py:

from django.forms import ModelForm
from models import TaskList, TaskCategory, SubTask

class SubTaskForm(ModelForm):
    class Meta:
        model = SubTask

处理程序:

class CreateSubTask(BaseHandler):
    def get(self):
        self.render_template('index.html', {'form':SubTaskForm})

app = webapp2.WSGIApplication([('/', IndexPage),
                               ('/subtask', CreateSubTask), ],
                              debug=True)

我得到的错误是: AttributeError: type object 'SubTask' has no attribute '_meta'

我的问题:有没有办法可以将 django 模型表单与 ndb 一起使用,或者如果我犯了一个错误,那是什么?如果不是 django modelforms,ndb 模型是否适用于 WTForms?

下面附上堆栈跟踪:

ERROR    2012-06-23 03:25:58,770 wsgi.py:189] 
Traceback (most recent call last):
  File "/homeproject/expts/google_appengine/google/appengine/runtime/wsgi.py", line 187, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/homeproject/expts/google_appengine/google/appengine/runtime/wsgi.py", line 225, in _LoadHandler
    handler = __import__(path[0])
  File "/homeproject/expts/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 676, in Decorate
    return func(self, *args, **kwargs)
  File "/homeproject/expts/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1858, in load_module
    return self.FindAndLoadModule(submodule, fullname, search_path)
  File "/homeproject/expts/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 676, in Decorate
    return func(self, *args, **kwargs)
  File "/homeproject/expts/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1722, in FindAndLoadModule
    description)
  File "/homeproject/expts/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 676, in Decorate
    return func(self, *args, **kwargs)
  File "/homeproject/expts/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1665, in LoadModuleRestricted
    description)
  File "/homeproject/expts/google_appengine/tdl/main.py", line 18, in <module>
    from views import IndexPage, CreateSubTask
  File "/homeproject/expts/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 676, in Decorate
    return func(self, *args, **kwargs)
  File "/homeproject/expts/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1858, in load_module
    return self.FindAndLoadModule(submodule, fullname, search_path)
  File "/homeproject/expts/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 676, in Decorate
    return func(self, *args, **kwargs)
  File "/homeproject/expts/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1722, in FindAndLoadModule
    description)
  File "/homeproject/expts/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 676, in Decorate
    return func(self, *args, **kwargs)
  File "/homeproject/expts/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1665, in LoadModuleRestricted
    description)
  File "/homeproject/expts/google_appengine/tdl/views.py", line 4, in <module>
    from forms import SubTaskForm
  File "/homeproject/expts/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 676, in Decorate
    return func(self, *args, **kwargs)
  File "/homeproject/expts/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1858, in load_module
    return self.FindAndLoadModule(submodule, fullname, search_path)
  File "/homeproject/expts/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 676, in Decorate
    return func(self, *args, **kwargs)
  File "/homeproject/expts/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1722, in FindAndLoadModule
    description)
  File "/homeproject/expts/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 676, in Decorate
    return func(self, *args, **kwargs)
  File "/homeproject/expts/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1665, in LoadModuleRestricted
    description)
  File "/homeproject/expts/google_appengine/tdl/forms.py", line 4, in <module>
    class SubTaskForm(ModelForm):
  File "/homeproject/expts/google_appengine/lib/django_1_3/django/forms/models.py", line 205, in __new__
    opts.exclude, opts.widgets, formfield_callback)
  File "/homeproject/expts/google_appengine/lib/django_1_3/django/forms/models.py", line 145, in fields_for_model
    opts = model._meta
AttributeError: type object 'SubTask' has no attribute '_meta'
4

3 回答 3

5

请不要使用 djangoforms - Python 2.7 App Engine 运行时不支持它。

于 2012-06-23T15:42:57.533 回答
2

除了上面的注释,如果你没有在 main.py 文件中提供设置,djangoforms 将不会呈现。除此之外,您需要添加一个中间件:

'google.appengine.ext.ndb.NdbDjangoMiddleware'

做到以上两点就可以解决问题。

于 2012-06-23T09:10:12.687 回答
0

我从来没有使用过 django 表单,所以可能缺少一些魔法(它可能无法使它工作......),但这条线看起来是错误的:

self.render_template('index.html', {'form':SubTaskForm})

您将类传递给您的模板,而不是它的实例:

self.render_template('index.html', {'form':SubTaskForm()})
于 2012-06-23T08:38:09.527 回答