我有一个非常简单的模型表单,但由于某种原因,代码无法syncdb并抛出错误:AttributeError:'module'对象没有属性'CharField'
代码如下(在我的models.py中):
from django.db import models
from django.forms import ModelForm, Textarea, forms
from django.forms.fields import DateField, ChoiceField, MultipleChoiceField
class SubmitJobDTP(models.Model):
SERVICE_CHOICES = (
('blog', (
('DTP1', 'cccccccccccccccccc: GBP 65.00'),
('DTP2', 'vvvvvvvvvvvvvvvvvv: GBP 110.00'),
('DTP3', 'bbbbbbbbbbbbbbbbbb: GBP 175.00'))
)
)
package = models.CharField(max_length=5, choices=SERVICE_CHOICES)
firstname = models.CharField(max_length=25)
lastname = models.CharField(max_length=25)
contact_number = models.CharField(max_length=25)
email_address = models.EmailField()
attachment_1 = models.FileField(upload_to='uploadir')
attachment_2 = models.FileField(upload_to='uploadir')
attachment_3 = models.FileField(upload_to='uploadir')
attachment_4 = models.FileField(upload_to='uploadir')
attachment_5 = models.FileField(upload_to='uploadir')
comments = models.CharField(max_length=150)
class SubmitJobForm(ModelForm):
attachment_1 = forms.FileField(label='Attach file 1',required=False)
attachment_2 = forms.FileField(label='Attach file 2',required=False)
attachment_3 = forms.FileField(label='Attach file 3',required=False)
attachment_4 = forms.FileField(label='Attach file 4',required=False)
attachment_5 = forms.FileField(label='Attach file 5',required=False)
package = forms.CharField(required=False)
firstname = forms.CharField(required=False)
lastname = forms.CharField(required=False)
contact_number = forms.IntegerField(required=False)
email_address = forms.EmailField(required=False)
class Meta:
model = SubmitJobDTP
fields = ('package', 'first name', 'last name', 'contact_number',
'email_address', 'comments', 'attachment_1', 'attachment_2',
'attachment_3', 'attachment_4', 'attachment_5')
已开启dpasted代码:http://dpaste.com/607823/
我想知道问题是什么:modelform syncdb中的FileField是正确的,但其他字段:CharField,IntegerField和EmailField似乎不起作用。我在模型表单上阅读过django文档,但我似乎无法找到与此错误特别相关的任何内容。
任何建议都会得到很多关注。
答案 0 :(得分:5)
您实际上并未导入表单模块。
>>> from django.forms import forms
>>> forms.CharField
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'module' object has no attribute 'CharField'
>>> from django import forms
>>> forms.CharField
<class 'django.forms.fields.CharField'>
因此,您希望将导入更改为以下内容:
from django import forms
然后引用ModelForm:
class SubmitJobForm(forms.ModelForm):
答案 1 :(得分:0)
您可以在forms.py中使用
来自django.forms导入表单
而不是这个用途
来自django导入表单的