django model form
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.
You're not actually importing the forms module.
>>> 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'>
So, you want to change your imports to the following:
from django import forms
Then to reference a ModelForm:
class SubmitJobForm(forms.ModelForm):
You may using in top of forms.py
from django.forms import forms
instead of this use
from django import forms
精彩评论