【问题标题】:Create error message datefield创建错误消息日期字段
【发布时间】:2017-03-09 19:43:06
【问题描述】:

我想为以下表单创建错误消息:

class ExaminationCreateForm(forms.ModelForm):
class Meta:
    model = Examination
    fields = ['patient', 'number_of_examination', 'date_of_examination']

型号:

class Patient(models.Model):
    patientID = models.CharField(max_length=200, unique=True, help_text='Insert PatientID')
    birth_date = models.DateField(auto_now=False, auto_now_add=False, help_text='YYYY-MM-DD')
    gender = models.CharField(max_length=200,choices=Gender_Choice, default='UNDEFINED')

class Examination(models.Model):
    number_of_examination = models.IntegerField(choices=EXA_Choices)
    patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
    date_of_examination = models.DateField(auto_now=False, auto_now_add=False, help_text='YYYY-MM-DD')

每位患者有 2 次检查(检查次数 = 选择 1 或 2),并且当第二次检查的日期

解决方案:`

def clean_date_of_examination(self):
        new_exam = self.cleaned_data.get('date_of_examination')
        try:
            old_exam = Examination.objects.get(patient=self.cleaned_data.get('patient'))
        except Examination.DoesNotExist:
            return new_exam

        if old_exam:
            if old_exam.date_of_examination > new_exam:
                raise forms.ValidationError("Second examination should take place after first examination")
        return new_exam`

【问题讨论】:

  • 你能展示你的模型吗?
  • 是的,请分享您的模型,因为我认为我的回答可能是错误的!
  • 我不太确定你想要达到什么目的,但我觉得我的回答是错误的,因为你的问题不是很明确!
  • 当第二次考试的日期早于第一次考试的日期时,它应该给出错误。这就是我想要做的。
  • 我认为这是最后一个 if 循环。 pat1.date_of_examination 的查询不正确。

标签: python django django-forms django-validation


【解决方案1】:
def clean_date_of_examination(self):
    new_exam = self.cleaned_data.get('date_of_examination')
    old_exam = Examination.objects.get(patient = self.cleaned_data.get('Patient'))
    if old_exam:
        if old_exam.date_of_examination > new_exam.date_of_examination:
            raise forms.ValidationError("Second examination should take place after first examination")
    return data

【讨论】:

  • 我在您的定义中添加了“try/except”语句,但出现错误:'datetime.date' 对象对于最后一个 if 语句没有属性 'date_of_examination' 我也用 new_exam 替换数据是这对吗?
  • 有问题!而不是“new_exam.date_of_examination”应该是“new_exam”
  • 是的,在添加“try/except”语句并修复最后一个“if ...”后它起作用了。非常感谢。为你点赞!
【解决方案2】:
def clean_date_of_examination(self):
    # Where 'data' is used?
    date_of_exam = self.cleaned_data['date_of_examination']

    try:
        pat1 = Patient.object.get(examination__number_of_examination=1, date_of_examination=date_of_exam)
    except Patiens.DoesNotExist:
        # Patient 1 with given query doesn't exist. Handle it!
    try:
        pat2 = Patient.object.get(examination__number_of_examination=2, date_of_examination=date_of_exam)
    except Patiens.DoesNotExist:
        # Patient 2 with given query doesn't exist. Handle it!

    if pat2.date_of_examination < pat1.date_of_examination:
         raise forms.ValidationError("Second examination should take place after first examination")`
    return data`

【讨论】:

  • 应该给你我的模型对不起!更新了我的问题:)
  • data 是什么?这是通过您的表格传递给这个日期的患者?
  • 说实话:我刚刚在 Django 文档中看到它,并认为这是返回某些东西所必需的。但也许这只是无稽之谈。我已经尝试过您的代码,但要获得 number_of_examination 它应该是“examination__number_of_examination”,当我编辑它时它说“患者匹配查询不存在”
  • 这意味着带有number_of_examination=1date_of_examination='date-from-form'Patient 记录不存在。更新了我的答案以捕获这些异常
猜你喜欢
  • 1970-01-01
  • 2016-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多