【问题标题】:Validating blank django form field not working验证空白 django 表单字段不起作用
【发布时间】:2020-09-13 03:06:12
【问题描述】:

我总是使用以下代码来验证表单以防止提交空白表单。它总是在 Django 1.8 中工作,但由于某种原因在 Django 2.2 中不工作。

这是表格

class CreateForm(forms.ModelForm):
    class Meta:
        model = Device
        fields = ['category', 'item_name', 'quantity']

    def clean_category(self):
        category = self.cleaned_data.get('category')
        if category == '':
            raise forms.ValidationError('This field is required')
        return category


    def clean_item_name(self):
        item_name = self.cleaned_data.get('item_name')
        if item_name == '':
            raise forms.ValidationError('This field is required')
        return item_name

这是模型

class Device(models.Model):
    category = models.CharField(max_length=50, blank=True, null=True)
    item_name = models.CharField(max_length=50, blank=True, null=True)
    quantity = models.IntegerField(default='0', blank=False, null=True)

谢谢

【问题讨论】:

  • 可以分享一下相关型号吗?
  • 为什么不在您正在使用的字段中指定min_length
  • 你有没有在你的模型字段中设置blank=True
  • 我编辑了问题并添加了模型。我不想使用模型验证。

标签: django django-forms


【解决方案1】:

由于您在模型中将这些字段指定为 blank=Truenull=True,因此请更改这些属性

class Device(models.Model):
    category = models.CharField(max_length=50, blank=False, null=False)

或者默认情况下,如果您不指定这些 blanknull 属性,那么默认情况下它将为 false,因此这也应该有效

class Device(models.Model):
    category = models.CharField(max_length=50)

EDIT 基于评论: 就像威廉说你需要检查无。你可以这样做。

def clean_category(self):
    category = self.cleaned_data.get('category')
    if not category:
        raise forms.ValidationError('This field is required')
    return category

【讨论】:

  • 感谢您的评论。我也明白这一点。我很抱歉,我的问题似乎不是很清楚。我想了解为什么验证不起作用。它适用于 Django 1.8。 Django 2.2 是否有任何更改使其无法正常工作?
  • 伟大的 arjun 和 @Willem 这行得通:def clean_category(self): category = self.cleaned_data.get('category') if not category: raise forms.ValidationError('This field is required') return category
  • @simplyvic:比通常情况下它是None,因为空字符串和None 是“错误”值。
【解决方案2】:

我认为问题在于您没有检查None,但尽管如此。我认为你的目标是自己做太多的工作。您可以指定该字段为required=True [Django-doc],这将:

默认情况下,每个 Field 类都假定该值是必需的,因此如果您传递一个空值 - None 或空字符串 ("") - 那么clean() 将引发ValidationError 异常。

所以我们可以用:

class CreateForm(forms.ModelForm):
    category = forms.CharField(required=True, max_length=50)
    item_name = forms.CharField(required=True, max_length=50)

    class Meta:
        model = Device
        fields = ['category', 'item_name', 'quantity']

话虽如此,指定blank=True [Django-doc] 是相当“奇怪”的,因为这实际上意味着模型表单中不需要该字段。 blank=True 并不意味着允许空字符串,因为即使使用blank=False,您也可以在字段中存储空字符串。 ModelForm 将根据它“包装”的模型定义(大部分)其验证,这意味着如果您更好地定义模型,您将删除大量样板代码。因此我建议删除blank=True

【讨论】:

  • 我知道这种方法就像在应用程序中添加更多不必要的代码,因为模型可以处理这些。我想了解为什么它不起作用。从 1.8 到 django 2.2 是否有任何更改导致这种类型的验证不起作用?
  • @simplyvic:可能是因为它不在.cleaned_data 中,所以您应该检查None(如答案顶部所写)。
  • 我也检查了None,但没用。我意识到 None 与整数和 ForeignKey 字段一起使用
  • @simplyvic:print(item_name) 可能会有所帮助,从而检查传递给它的确切内容。
  • 这很好用。 def clean_category(self): category = self.cleaned_data.get('category') if not category: raise forms.ValidationError('This field is required') return category
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-10
  • 1970-01-01
  • 1970-01-01
  • 2019-03-13
  • 2020-11-03
相关资源
最近更新 更多