简体   繁体   中英

ModelForm has no model class specified in django

I have been following this site . I want to add an extra field email along with those available in model. I have

form.py

from django import forms from django.forms import ModelForm import django.forms

from 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

and the error I encountered is

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

You have a typo

class meta:

Should be

class Meta:

Note the case.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM