如何纠正定义表单时的错误.ModelForm小部件

时间:2017-12-20 13:47:17

标签: python django django-models django-forms

enter image description here

将窗口小部件分配给forms.ModelForm时出现以下错误。 请建议如何纠正此错误。

forms.py

from django import forms
from django.forms import ModelForm, CharField
from .models import *

class CreateTaskMaster(forms.ModelForm):
    class Meta():
        model  = TaskMaster
        fields = ["sid", "tasktype", "task_title", "task_description", 
           "datacenter", "priority", "sourceincident", "processingteam", 
                "duedate"]
    widgets = {
        'duedate': CharField(widget=forms.TextInput(attrs={'class': 'form-
                 control mr-sm-2'})),
                }

Model.py

Class TaskMaster(models.Model):
  sid = models.CharField(max_length=3)
  processor = models.ForeignKey(User,null=True)
  tasktype = models.ForeignKey(TaskTypeTable, null=True)
  task_title = models.TextField(null=True)
  task_description = models.TextField(null=True)
  datacenter = models.ForeignKey(DatacenterTable,null=True)
  priority = models.ForeignKey(PriorityTable, null=True)
  status = models.ForeignKey(StatusTable,default=1, null=True)
  pid = models.IntegerField(null=True)
  sourceincident = models.URLField(null=True)
  errorincident = models.URLField(null=True)
  processingteam = models.ForeignKey(TeamTable, null=True)
  createddate = models.DateField(("Date"), default=datetime.date.today)
  duedate = models.DateField(("Date"), default=datetime.date.today)
  istaskactive = models.BooleanField(default=True)

  class Meta:
    ordering = ('duedate',)

1 个答案:

答案 0 :(得分:0)

尝试以下方式,仅替换表单类:

class CreateTaskMaster(forms.ModelForm):
    class Meta():
        model  = TaskMaster
        fields = ["sid", "tasktype", "task_title", "task_description", 
           "datacenter", "priority", "sourceincident", "processingteam", 
                "duedate"]
         widgets = {'duedate': forms.TextInput(attrs={'class': 'form-
                 control mr-sm-2'}),
                }