我有一個很簡單的模型的形式,但由於某些原因,該代碼不能執行syncdb和引發錯誤:AttributeError的:「模塊」對象沒有屬性「CharField」django的模型形式
的代碼如下(在我的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/
我不知道這個問題可能是什麼:FileField或在的ModelForm執行syncdb的正確,但其他領域:CharField,IntegerField和EmailField似乎不起作用。我已經閱讀了模型表單上的django文檔,我似乎無法找到與此錯誤特別相關的任何內容。
任何建議都會大大增加。
我建議讀了(https://docs.djangoproject.com/en/dev/ref/django-admin/#syncdb)什麼'syncdb'確實 - 它不會觸摸'ModelForm'類,它會創建由'django.db.models.Model'子類定義的表。嘗試運行'./manage.py shell'或任何其他管理命令,你會發現你的代碼中有一個import語法錯誤,'from django.forms import ModelForm,Textarea,forms' –
@James,你正在使用表單。 ModelForm旨在最大限度地減少必須編寫的自定義代碼的數量。通過聲明模型中已經存在的每個字段,您正在有效地創建一個標準表單。閱讀更多關於ModelForms,並節省您的時間和錯誤。 –