6

嗨#1人,Django的:基於類視圖模型對象「有沒有屬性‘_meta’」

我與基於類的意見和試驗現場工作,我跟着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' 

回答

9

變化:

class Project(models.Manager) 

到:

class Project(models.Model) 

因爲models.Model是實際的類必須擴展創建自定義模型,經理們實際使用覆蓋數據庫操作參與模型

+0

是否還有其他一些缺失?因爲我按照您的建議做了同樣的事情,所以不會刪除屬性錯誤。 – tilaprimera

+0

應該導入什麼模型的'models.Model'?有很多選擇,但似乎我的自定義模型不適合其中任何一個。 – KasparTr

相關問題