ModelForm class Contact has no attribute '_meta'

Klanestro picture Klanestro · Oct 21, 2012 · Viewed 7.6k times · Source

When I inherit ModelForm in my ContactForm I get this error, when I had it so modelform did not inherit from modelform no errors just no form on the html page. I really can't figure this one out

AttributeError at /addacontact class Contact has no attribute '_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'

this is 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

Answer

K Z picture K Z · Oct 21, 2012

You need to inherit models.Model for your model classes:

class Contact:
...

should be:

class Contact(models.Model):
...

models.Model has _meta attribute, which Contact model will inherit and will be used in when generating ModelForm.