嗨Stackoverflow人,
我正在使用基于类的视图和测试网站,我按照documentation设置了基于类的视图。 对于项目站点(基于下面的项目模型),我只想为下面的简单项目模型创建一个快速CRUD应用程序。
models.py
class Project(models.Manager):
name = models.CharField(_('Name of the Project'), max_length = 100,)
slug = models.SlugField(max_length=100,)
...
views.py
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.core.urlresolvers import reverse_lazy
from project.models import Project
class ProjectCreate(CreateView):
model = Project
class ProjectUpdate(UpdateView):
model = Project
当我现在加载应该调用\project\add
类的ProjectCreate
时,我收到以下错误:
类型对象'Project'没有属性'_meta'
我不明白为什么。模型类通常不需要元信息。在其他基于类的项目中,我没有遇到这个问题。
感谢您的建议!
回溯
Django Version: 1.4.1
Python Version: 2.7.1
Traceback:
File "/Users/neurix/Development/vir_project_2.0/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/neurix/Development/vir_project_2.0/lib/python2.7/site-packages/django/views/generic/base.py" in view
48. return self.dispatch(request, *args, **kwargs)
File "/Users/neurix/Development/vir_project_2.0/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
69. return handler(request, *args, **kwargs)
File "/Users/neurix/Development/vir_project_2.0/lib/python2.7/site-packages/django/views/generic/edit.py" in get
168. return super(BaseCreateView, self).get(request, *args, **kwargs)
File "/Users/neurix/Development/vir_project_2.0/lib/python2.7/site-packages/django/views/generic/edit.py" in get
130. form_class = self.get_form_class()
File "/Users/neurix/Development/vir_project_2.0/lib/python2.7/site-packages/django/views/generic/edit.py" in get_form_class
89. return model_forms.modelform_factory(model)
File "/Users/neurix/Development/vir_project_2.0/lib/python2.7/site-packages/django/forms/models.py" in modelform_factory
407. return form_metaclass(class_name, (form,), form_class_attrs)
File "/Users/neurix/Development/vir_project_2.0/lib/python2.7/site-packages/django/forms/models.py" in __new__
206. opts.exclude, opts.widgets, formfield_callback)
File "/Users/neurix/Development/vir_project_2.0/lib/python2.7/site-packages/django/forms/models.py" in fields_for_model
146. opts = model._meta
Exception Type: AttributeError at /project/add/
Exception Value: type object 'Project' has no attribute '_meta'
答案 0 :(得分:10)
变化:
class Project(models.Manager)
为:
class Project(models.Model)
因为models.Model
是实际类,您必须扩展以创建自定义模型,而管理器实际上用于覆盖模型所涉及的数据库操作。