ModelForm 的 Django TextField max_length 验证

发布于 2024-09-08 09:09:49 字数 743 浏览 7 评论 0原文

Django 在验证 ModelForm 时不考虑 TextField 模型字段的 max_length 属性。

所以我定义了一个继承自models.TextField的LimitedTextField,并添加了类似于models.CharField的验证位:

from django.core import validators

class LimitedTextField(models.TextField):
    def __init__(self, *args, **kwargs):
        super(LimitedTextField, self).__init__(*args, **kwargs)
        self.max_length = kwargs.get('max_length')
        if self.max_length:
            self.validators.append(validators.MaxLengthValidator(self.max_length))

    def formfield(self, **kwargs):
        defaults = {'max_length': self.max_length}
        defaults.update(kwargs)
        return super(LimitedTextField, self).formfield(**defaults)

但这仍然对ModelForm验证没有影响。

我缺少什么?

Django does not respect the max_length attribute of TextField model field while validating a ModelForm.

So I define a LimitedTextField inherited from the models.TextField and added validation bits similar to models.CharField:

from django.core import validators

class LimitedTextField(models.TextField):
    def __init__(self, *args, **kwargs):
        super(LimitedTextField, self).__init__(*args, **kwargs)
        self.max_length = kwargs.get('max_length')
        if self.max_length:
            self.validators.append(validators.MaxLengthValidator(self.max_length))

    def formfield(self, **kwargs):
        defaults = {'max_length': self.max_length}
        defaults.update(kwargs)
        return super(LimitedTextField, self).formfield(**defaults)

But this still has no affect on ModelForm validation.

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鯉魚旗 2024-09-15 09:09:49

从 Django 1.2 开始,这可以通过模型级别的验证器来完成,如下所述:
https://docs.djangoproject.com/en/stable/ref/validators/

from django.core.validators import MaxLengthValidator

class Comment(models.Model):
    comment = models.TextField(validators=[MaxLengthValidator(200)])

从 Django 1.7 开始,您可以使用仅在客户端强制执行的 max_length参见此处

As of Django 1.2 this can be done by validators at model level, as explained here:
https://docs.djangoproject.com/en/stable/ref/validators/

from django.core.validators import MaxLengthValidator

class Comment(models.Model):
    comment = models.TextField(validators=[MaxLengthValidator(200)])

Since Django 1.7, you can use max_length which is only enforced in client side. See here

沙与沫 2024-09-15 09:09:49

您可以通过使用 Textarea 小部件定义 CharField 来强制 TextField 的最大长度,如下所示:

class MyClass(models.Model):
    textfield = models.TextField()

class MyForm(forms.ModelForm):
    textfield = forms.CharField(
        max_length = 50,
        widget = forms.Textarea
    )

    class Meta:
        model = MyClass
        fields = ('textfield',)

You can enforce a max length for a TextField by defining a CharField with a Textarea widget like this:

class MyClass(models.Model):
    textfield = models.TextField()

class MyForm(forms.ModelForm):
    textfield = forms.CharField(
        max_length = 50,
        widget = forms.Textarea
    )

    class Meta:
        model = MyClass
        fields = ('textfield',)
心意如水 2024-09-15 09:09:49

无需从 Django 2.x 的验证器导入 MaxLengthValidator

from django.db import models
class Comment(models.Model):
  comment = models.TextField(max_length=200)

No need to import MaxLengthValidator from validators for Django 2.x

from django.db import models
class Comment(models.Model):
  comment = models.TextField(max_length=200)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文