2012-10-21 57 views
3

當我在我的ContactForm中繼承ModelForm時,我得到了這個錯誤,當我這樣做時,modelform沒有從modelform繼承,沒有錯誤,只是在html頁面上沒有任何表單。我真的想不通這一個ModelForm類聯繫人沒有屬性'_meta'

AttributeError的在/ addacontact 類跟有沒有屬性 '_meta'

Environment: 


Request Method: GET 
Request URL: http://127.0.0.1:8000/addacontact 

Django Version: 1.4.2 
Python Version: 2.7.3 
Installed Applications: 
('django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.sites', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
'django.contrib.admin', 
'django.contrib.admindocs', 
'south', 
'sekizai') 
Installed Middleware: 
('django.middleware.common.CommonMiddleware', 
'django.contrib.sessions.middleware.SessionMiddleware', 
'django.middleware.csrf.CsrfViewMiddleware', 
'django.contrib.auth.middleware.AuthenticationMiddleware', 
'django.contrib.messages.middleware.MessageMiddleware', 
'django.middleware.clickjacking.XFrameOptionsMiddleware') 


Traceback: 
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/core/handlers/base.py" in get_response 
    89.      response = middleware_method(request) 
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/middleware/common.py" in process_request 
    67.    if (not urlresolvers.is_valid_path(request.path_info, urlconf) and 
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/core/urlresolvers.py" in is_valid_path 
    531.   resolve(path, urlconf) 
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/core/urlresolvers.py" in resolve 
    420.  return get_resolver(urlconf).resolve(path) 
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/core/urlresolvers.py" in resolve 
    298.    for pattern in self.url_patterns: 
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/core/urlresolvers.py" in url_patterns 
    328.   patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) 
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/core/urlresolvers.py" in urlconf_module 
    323.    self._urlconf_module = import_module(self.urlconf_name) 
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/utils/importlib.py" in import_module 
    35.  __import__(name) 
File "/home/brian/projects/steprider/steprider/urls.py" in <module> 
    2. from steprider.views import Add_a_contact 
File "/home/brian/projects/steprider/steprider/views.py" in <module> 
    9. from salesflow.forms import * 
File "/home/brian/projects/steprider/salesflow/forms.py" in <module> 
    13. class ContactForm(ModelForm): 
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/forms/models.py" in __new__ 
    206.          opts.exclude, opts.widgets, formfield_callback) 
File "/home/brian/virt_env/virt_step/local/lib/python2.7/site-packages/Django-1.4.2-py2.7.egg/django/forms/models.py" in fields_for_model 
    146.  opts = model._meta 

Exception Type: AttributeError at /addacontact 
Exception Value: class Contact has no attribute '_meta' 

這是forms.py

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
# 
# steprider.salesflow.forms.py 
# 
# Copyright 2012 BRIAN SCOTT CARPENTER <[email protected]> 

from django.forms import ModelForm 
from salesflow import models 
from django import forms 
from django.template.defaultfilters import slugify 

class ContactForm(ModelForm): 
    description = forms.CharField(widget=forms.Textarea) 

    class Meta: 
     model = models.Contact 
     exclude = ("user","slug") 

    def save(self, user): 
     contact = super(ContactForm, self).save(commit=False) 
     contact.user = ser 
     contact.save() 
     return contact 

class Contact_History_Form: 

    class Meta: 
     model = models.Contact_History 
     exclude = ("user","slug") 

models.py

from django.db import models 
from django.contrib.auth.models import User 
from django_extensions.db.fields import AutoSlugField 



# Create your models here. 
class Contact: 
    user = models.ForeignKey(User) 
    business = models.CharField(max_length=50) 
    title = models.CharField(max_length=50) 
    name = models.CharField(max_length=50) 
    address = models.CharField(max_length=50) 
    city = models.CharField(max_length=60) 
    state_province = models.CharField(max_length=30) 
    country = models.CharField(max_length=50) 
    website = models.URLField() 
    email = models.EmailField() 
    description = models.CharField(max_length=3000) 
    slug = models.SlugField(max_length=128) 
    slug = AutoSlugField(('slug'), max_length=128, unique=True, populate_from=('business',)) 


    def __unicode__(self): 
     return self.slug 


class Contact_History: 
    user = models.ForeignKey(User) 
    contact = models.ForeignKey('Contact') 

    number_of_emails_sent = models.IntegerField() 
    last_email_sent = models.DateField() 

    phone_notes = models.CharField(max_length=2000) 


    slug = models.SlugField(max_length=128) 
    slug = AutoSlugField(('slug'), max_length=128, unique=True, populate_from=('name',)) 


    def __unicode__(self): 
     return self.slug 

回答

8

您需要爲模型類繼承models.Model

class Contact: 
... 

應該是:

class Contact(models.Model): 
... 

models.Model具有_meta屬性,該屬性Contact模型將繼承和將生成ModelForm時在使用。

+0

非常感謝,你能想出一個很好的指導來理解追溯?所以我可以避免這種錯誤。 – Klanestro

+0

@Klanestro我認爲[Werkzeug](http://werkzeug.pocoo.org)是調試Django(和金字塔等)應用程序的寶貴工具。您可以在*瀏覽器*調試stacktrace。雖然在使用Django更深層次之後,立即連接點需要更少的時間(或調試)。 –

+0

所以我的模型設置包括類ClassName(models.Model):我仍然遇到這個問題... –

相關問題