Django 模型形式
我有一个非常简单的模型形式,但由于某种原因,代码无法同步数据库并抛出错误: AttributeError: 'module' object has no attribute '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')
A dpasted代码位于: http://dpaste.com/607823/
我想知道问题出在哪里是:模型表单同步数据库中的 FileField 正确,但其他字段:CharField、IntegerField 和 EmailField 似乎不起作用。我已阅读模型表单上的 django 文档,但似乎找不到与此错误特别相关的任何内容。
任何建议将不胜感激。
I have a very simple model form, but for some reason, the code fails to syncdb and throws an error: AttributeError: 'module' object has no attribute 'CharField'
the code is as follows (in my 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')
A dpasted code is on: http://dpaste.com/607823/
I wonder what the problem could be: The FileField in the modelform syncdb's correctly, but the other fields: CharField, IntegerField and EmailField do not seem to work. I have read the django docs on model form and I cannot seem to find anything particularly related to this error.
Any suggestions would be much apperciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您实际上并未导入表单模块。
因此,您希望将导入更改为以下内容:
然后引用 ModelForm:
You're not actually importing the forms module.
So, you want to change your imports to the following:
Then to reference a ModelForm:
您可以在 forms.py 顶部使用
from django.forms import forms
而不是使用这个
从 django 导入表单
You may using in top of forms.py
from django.forms import forms
instead of this use
from django import forms