无法导入form_for_model

时间:2015-10-22 07:46:43

标签: python django django-forms

这是forms.py

任何人都可以告诉我为什么我无法导入 form_for_model 我是否认为form_for_model是内置的?

from django import forms
from models import Publisher
from django.forms import form_for_model
PublisherForm = form_for_model(Publisher)
TOPIC_CHOICES = (
      ('general', 'General enquiry'),
      ('bug', 'Bug report'),
      ('suggestion', 'Suggestion'),
      )
class ContactForm(forms.Form):
           topic = forms.ChoiceField(choices=TOPIC_CHOICES)
           message = forms.CharField(widget=forms.Textarea())
           sender = forms.EmailField(required=False)
           def clean_message(self):
                     message = self.clean_data.get('message', '')
                     num_words = len(message.split())
                     if num_words < 4:
                         raise forms.ValidationError("Not enough words!")
                     return message

2 个答案:

答案 0 :(得分:1)

form_for_model已删除,您应使用ModelForm代替 check this already removed

from django.forms import ModelForm
from myapp.models import Publisher
....
....
# Create the form class.
class ContactForm(forms.Form):
           topic = forms.ChoiceField(choices=TOPIC_CHOICES)
           message = forms.CharField(widget=forms.Textarea())
           sender = forms.EmailField(required=False)
....
....

答案 1 :(得分:1)

form_for_model折旧使用ModelForm代替,

from django.forms import ModelForm
class ContactForm(ModelForm):
       topic = forms.ChoiceField(choices=TOPIC_CHOICES)
       ....

您可以在以下位置找到文档: https://docs.djangoproject.com/en/1.8/topics/forms/modelforms/#django.forms.ModelForm