TextField

36 views
Skip to first unread message

אורי

unread,
Jan 5, 2020, 10:25:34 PM1/5/20
to django...@googlegroups.com
Django users,

Is there a default max length for TextField which is enforced in the database? We are using PostgreSQL and I don't want users (hackers) to flood our database with megabytes of meaningless text.

Thanks,
Uri.

Mike Dewhirst

unread,
Jan 5, 2020, 11:12:16 PM1/5/20
to django...@googlegroups.com
On 6/01/2020 2:24 pm, אורי wrote:
> Django users,
>
> Is there a default max length for TextField which is enforced in the
> database? We are using PostgreSQL and I don't want users (hackers) to
> flood our database with megabytes of meaningless text.

Uri

In your model create a clean() method. If it exists [1] this is called
by django forms prior to a save. If you access the field via an API or
unit tests you have to call it specifically. In there you can test for a
maximum size - or anything else really.

class SomeModel(models.Model):

    long_text = models.TextField()

    def clean(self):
        txt = self.long_text or ""
        if len(txt) > 100000000:
            # this needs to be reported to the user because it will
prevent saving
            # happens automatically in the Admin ...
            raise ValidationError("Too much meaningless text")


[1]
https://docs.djangoproject.com/en/2.2/topics/forms/modelforms/#overriding-the-clean-method

Cheers

Mike


>
> Thanks,
> Uri.
> אורי
> u...@speedy.net <mailto:u...@speedy.net>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CABD5YeFq%3DUAGJSDvtMJetb4LknvGhCWtK1ie%3DEvUsJVB_n2B4g%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CABD5YeFq%3DUAGJSDvtMJetb4LknvGhCWtK1ie%3DEvUsJVB_n2B4g%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Abu Yusuf

unread,
Jan 5, 2020, 11:43:24 PM1/5/20
to django...@googlegroups.com

No, there is no limit for textfield. But you can do the hack using this:

from django.core.validators import MaxLengthValidator

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

Abu Yusuf

unread,
Jan 5, 2020, 11:43:49 PM1/5/20
to django...@googlegroups.com
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4b29a5e5-b328-3857-6d21-482e5018f5bc%40dewhirst.com.au.

אורי

unread,
Jan 6, 2020, 4:10:05 AM1/6/20
to django...@googlegroups.com
It's better to add a validator in the model:

text = models.TextField(verbose_name=_('your message'), max_length=50000, validators=[MaxLengthValidator(limit_value=50000)])

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4b29a5e5-b328-3857-6d21-482e5018f5bc%40dewhirst.com.au.

אורי

unread,
Jan 6, 2020, 4:11:35 AM1/6/20
to django...@googlegroups.com
Sorry, I didn't see that you already wrote that.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages