Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set min length for models.TextField()?

Tags:

django

There seem to be a max_length for CharField only. But how about min_length for a TextField(), is there a way to enforce 100 characters in the TextField?

like image 443
Houman Avatar asked Apr 05 '13 23:04

Houman


People also ask

What is the max length of TextField Django?

Django tip: Use models. TextField for storing long texts inside your models instead of models. CharField since CharField has a max length of 255 characters while TextField can hold more than 255 characters.

What is TextField in Python?

TextField is a field used to create an arbitrary amount of text characters in a database column defined by the Django ORM. The Django project has wonderful documentation for TextField and all of the other column fields. Note that TextField is defined within the django.

What is TextField in Django?

TextField is a large text field for large-sized text. TextField is generally used for storing paragraphs and all other text data. The default form widget for this field is TextArea.


2 Answers

You would use a custom validator, specifically MinLengthValidator

like image 197
Ngenator Avatar answered Oct 04 '22 22:10

Ngenator


When declaring a Django form

cell_phone=forms.CharField(max_length=10,min_length=10);

When rendering a form field in Django template

{% render_field form1.cell_phone  minlength="10" maxlength="10" %}

Underscore is confusing

like image 27
Aseem Avatar answered Oct 04 '22 22:10

Aseem