2015-02-06 87 views
0

我已經創建的視圖MyView的(CreateView的)和模型和形式,並得到一個錯誤:AttributeError的:對象類型「爲MyModel」有沒有屬性「_meta」

AttributeError: type object 'MyModel' has no attribute '_meta' 

這是我的觀點:

class MyView(CreateView): 
    model = MyModel 
    template_name = "myapp/template1.html" 

這裏是我的模型:

class MyModel(models.Model): 
    free_day = models.CharField(max_length=255) 
    name = models.CharField(max_length=255, blank=True, null=True) 

    class Meta: 
     verbose_name = _('MyModel') 
     verbose_name_plural = _('MyModels') 

    def __unicode__(self): 
     return self.free_day 

我的形式:

class MyInputForm(forms.ModelForm): 

    class Meta: 
     model = MyModel 
     fields = ('user', 'free_day',) 

這是我回溯

Internal Server Error: /freedays/addvacation/ 
Traceback (most recent call last): 
    File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response 
    response = wrapped_callback(request, *callback_args, **callback_kwargs) 
    File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/db/transaction.py", line 394, in inner 
    return func(*args, **kwargs) 
    File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/views/generic/base.py", line 69, in view 
    return self.dispatch(request, *args, **kwargs) 
    File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/views/generic/base.py", line 87, in dispatch 
    return handler(request, *args, **kwargs) 
    File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/views/generic/edit.py", line 203, in get 
    return super(BaseCreateView, self).get(request, *args, **kwargs) 
    File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/views/generic/edit.py", line 161, in get 
    form_class = self.get_form_class() 
    File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/views/generic/edit.py", line 119, in get_form_class 
    return model_forms.modelform_factory(model, fields=self.fields) 
    File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/forms/models.py", line 539, in modelform_factory 
    return type(form)(class_name, (form,), form_class_attrs) 
    File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/forms/models.py", line 284, in __new__ 
    opts.help_texts, opts.error_messages) 
    File "/home/mariusz/.virtualenvs/urlop/local/lib/python2.7/site-packages/django/forms/models.py", line 184, in fields_for_model 
    opts = model._meta 
AttributeError: type object 'MyModel' has no attribute '_meta' 
+0

請顯示* full * traceback。 – 2015-02-06 07:46:03

+0

我加了完整的回溯。 – mark 2015-02-06 07:50:13

+0

試用了你的代碼,它適用於我。你的'urls.py'中有什麼指向這個視圖? – 2015-02-06 08:55:48

回答

1

我不明白究竟爲什麼它給這個錯誤(這似乎爲MyModel實際上不是你的模型類 - 你重新定義了它在其他地方?),但有一個明顯的問題:您認爲應該參考您定義的表單,而不是直接的模型。

class MyView(CreateView): 
    form_class = MyInputForm 
+0

謝謝丹尼爾:) – mark 2015-02-06 09:14:02

-1

您是否嘗試過具有間的功能,看起來像這樣;

class MyModel(models.Model): 
    free_day = models.CharField(max_length=255) 
    name = models.CharField(max_length=255, blank=True, null=True) 

    def Meta(): 
     verbose_name = _('MyModel') 
     verbose_name_plural = _('MyModels') 

    def __unicode__(self): 
     return self.free_day 
+0

這不起作用。 – mark 2015-02-06 08:22:35

0

取而代之的是以下內容:

def get(request,self): 

你可以使用這樣的事情:

def get(self,request): 

其他的一切應該是蠻好的!

相關問題