Django 模型形式

发布于 2024-12-02 15:37:11 字数 2342 浏览 0 评论 0原文

我有一个非常简单的模型形式,但由于某种原因,代码无法同步数据库并抛出错误: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

衣神在巴黎 2024-12-09 15:37:11

您实际上并未导入表单模块。

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

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):
眼眸里的快感 2024-12-09 15:37:11

您可以在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文