ModelForm没有在django中指定的模型类

时间:2013-11-10 03:24:50

标签: python django

我一直关注这个site。我想添加一个额外的字段电子邮件以及模型中可用的电子邮件。我有

form.py

来自django导入表单 来自django.forms导入ModelForm import django.forms

来自new_db.models import Author,Publisher,Book

class ContactForm(forms.ModelForm):
    email = forms.EmailField(required=True,)

    class meta:
        model=Publisher
        fields=['first_name','last_name']

    def __init__(self, *args, **kwargs):
     if kwargs.get('instance'):
        email = kwargs['instance'].email
        kwargs.setdefault('initial', {})['email'] = email
     return super(ContactForm, self).__init__(*args, **kwargs)  

views.py

 class CreateContact(CreateView):
        model=Publisher
        template_name='CreatePublisher.html'
        form_class=ContactForm

我遇到的错误是

Exception Value:    ModelForm has no model class specified.
    Exception Location: C:\Python27\lib\site-packages\django\forms\models.py in __init__, line 238

1 个答案:

答案 0 :(得分:9)

你有一个错字

class meta:

应该是

class Meta:

注意这个案子。

更多https://docs.djangoproject.com/en/dev/topics/db/models/#meta-inheritance