ndb模型appengine的模型表格(djangoforms不适合我)

时间:2012-06-23 03:38:23

标签: python django google-app-engine django-forms app-engine-ndb

我使用的是webapp2,ndb模型 我试图使用django模型形式与ndb模型。以下是我的设置中的相关部分:

的app.yaml:

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

Models.py:

class SubTask(ndb.Model):

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

Forms.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:类型对象'SubTask'没有属性'_meta'

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

下面是stacktrace:

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'

3 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:2)

除上述评论外,如果您未在main.py文件中提供设置,则djangoforms将不会呈现。除此之外,您还需要添加中间件:

'google.appengine.ext.ndb.NdbDjangoMiddleware'

执行上述两项操作将解决问题。

答案 2 :(得分:0)

我从未使用过django表格,所以可能会遗漏一些魔法(并且它可能无法正常工作......),但这条线看起来不对:

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

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

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