Django: model object “has no attribute '_meta'” in class based view

旧街凉风 提交于 2019-12-06 23:59:29

问题


Hi Stackoverflow people,

I am working with class based views and for a test site, I followed the documentation to setup the class based views. For a project site (based on the project model below), I just want to create a quick CRUD application for the simple project model below.

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

When I now load \project\add, which should call the ProjectCreate class, I get the following error: type object 'Project' has no attribute '_meta'

I do not understand why. The model class normally does not require meta information. In other class based projects, I did not encounter this problem.

Thank you for your suggestions!

Traceback

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'

回答1:


Change:

class Project(models.Manager)

to:

class Project(models.Model)

because models.Model is actual class you must extend to create custom models, and managers are actually used to override database operations models are involved in.



来源:https://stackoverflow.com/questions/12338091/django-model-object-has-no-attribute-meta-in-class-based-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!