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)])
On Mon, Jan 6, 2020 at 10:11 AM Mike Dewhirst <mi...@dewhirst.com.au> wrote: > 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+unsubscr...@googlegroups.com > > <mailto:django-users+unsubscr...@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 > >. > > -- > 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+unsubscr...@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 > . > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACNsr2_jZsdT4_UEN029z2DG7NdTNf%2BZO3ey2Q%3DBwRjHEj8dnw%40mail.gmail.com.