Django:模型对象在基于类的视图中“没有属性‘_meta’”

问题描述 投票:0回答:1

大家好 Stackoverflow,

我正在使用基于类的视图,对于测试站点,我按照 文档 设置基于类的视图。 对于一个项目站点(基于下面的项目模型),我只想为下面的简单项目模型创建一个快速的 CRUD 应用程序。

模型.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'
python django django-class-based-views
1个回答
14
投票

改变:

class Project(models.Manager)

至:

class Project(models.Model)

因为

models.Model
是您必须扩展才能创建自定义模型的实际类,而管理器实际上用于覆盖模型所涉及的数据库操作。

© www.soinside.com 2019 - 2024. All rights reserved.