This question already has an answer here:
'module' object has no attribute 'form_for_model'
                                
                                    (1个答案)
                                
                        
                                4年前关闭。
            
                    
这是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

最佳答案

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)
....
....

关于python - 无法导入form_for_model ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33275792/

10-13 07:13