使用 and or 规则的 Django 验证

Django Validation with and or rule

条件应该是只有这个字段填写其他字段不需要填写。现在使用此代码我必须填写所有字段。

 if (self.start==None and self.end==None) or (self.running==None  and self.walking==None ) :
        raise ValidationError(
            {
                "start": _("Need to be filled"),
                 "end": _("Need to be filled"),
                "running": _("Need to be filled"),
                "walking": _("Need to be filled")
            }
        )

试试这个:

if self.start==None and self.end==None and self.running==None and self.walking==None:
   raise ValidationError(
            {
                "start": _("Need to be filled"),
                "end": _("Need to be filled"),
                "running": _("Need to be filled"),
                "walking": _("Need to be filled")
            }
        )

if (self.start!=None or self.end!=None) and (self.running!=None or self.walking!=None):
   raise ValidationError(
            {
                "running": _("Don't need to be filled"),
                "walking": _("Don't need to be filled")
            }
        )

if self.start!=None and self.end==None:
   raise ValidationError(
            {
                "end": _("Need to be filled")
            }
        )

if self.start==None and self.end!=None:
   raise ValidationError(
            {
                "start": _("Need to be filled")
            }
        )

if self.running!=None and self.walking==None:
   raise ValidationError(
            {
                "walking": _("Need to be filled")
            }
        )

if self.running==None and self.walking!=None:
   raise ValidationError(
            {
                "running": _("Need to be filled")
            }
        )