Django表单:警告:root:无法解析表单字段

时间:2016-10-12 07:48:05

标签: python django django-models django-forms

我在python 2.7中使用django v1.8

models.py的表格中,我有

class Diagnosis(models.Model):
    diagnosis_circumstances = models.CharField(max_length=150)
    diagnosis_circumstances_date = models.DateField('Date of diagnosis',null=True,blank=True)
    date_of_input= models.DateField(null=True,blank=True)
    pub_date = models.DateTimeField(auto_now=True)
    author = models.ForeignKey(User)
    patient = models.ForeignKey(Demographic)

当我尝试在pub_date中显示forms.py字段时:

class DiagnosisForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):

        super(DiagnosisForm, self).__init__(*args, **kwargs)
        self.helper=FormHelper(form=self)
        self.helper.field_class = 'col-md-8'
        self.helper.label_class = 'col-md-3'

        # self.helper.form_class = 'forms-horizontal'
        self.helper.layout = Layout(
            Fieldset(
                '<b>Latest update</b>',
                Div(
                    Div('pub_date', css_class='col-md-8'),
                    css_class='row',
                ),
            ),
            FormActions(
                Submit('submit', "Save changes"),
                Submit('cancel',"Cancel")
            ),
        )
        self.helper.form_tag = False
        self.helper.form_show_labels = True

    class Meta:
        model = Diagnosis
        exclude = ['patient', 'author']
        list_display = ('patient', 'pub_date', 'author')

我收到消息:

WARNING:root:Could not resolve form field 'pub_date'.
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\crispy_forms\utils.py", line 92, in render_f
ield
    field_instance = form.fields[field]
KeyError: u'pub_date'

你知道如何解决这个问题吗?

0 个答案:

没有答案