Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
like image 861
neurix Avatar asked Sep 09 '12 09:09

neurix


1 Answers

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.

like image 156
Rostyslav Dzinko Avatar answered Nov 15 '22 16:11

Rostyslav Dzinko