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

2019-07-31 11:36发布

嗨#1人,

我与基于类视图工作和试验基地,我跟着文档设置基于类的意见。 对于一个项目工地(基于下面的项目模型),我只是想为下面的简单项目模型快速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类,我得到以下错误: 对象类型“项目”有没有属性“_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'

Answer 1:

更改:

class Project(models.Manager)

至:

class Project(models.Model)

因为models.Model是实际的类必须扩展创建自定义模型,经理们实际上用于覆盖数据库操作模式都参与英寸



文章来源: Django: model object “has no attribute '_meta'” in class based view