# Django change user models minimum length

**URL:** <https://forum.djangoproject.com/t/django-change-user-models-minimum-length/2595>\
**Category:** Using Django\
**Created:** [May 23, 2020, 9:33pm UTC](https://forum.djangoproject.com/t/django-change-user-models-minimum-length/2595 "2020-05-23T21:33:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![eqmunir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/eqmunir/32/1073_2.png) [@eqmunir](https://forum.djangoproject.com/u/eqmunir)\
**Post date:** [May 23, 2020, 9:33pm UTC](https://forum.djangoproject.com/t/django-change-user-models-minimum-length/2595/1 "2020-05-23T21:33:24Z")

</div>

Hello, in my project, I want to change the minimum length of the username field on the built in user model.

To do this, should I inherit from the **AbstractUser** , and override the username property to have the minimum length I need, or should I inherit from the **AbstractBaseUser** class? Which method is best to accomplish this? Thanks.

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [May 23, 2020, 10:12pm UTC](https://forum.djangoproject.com/t/django-change-user-models-minimum-length/2595/2 "2020-05-23T22:12:54Z")

</div>

Hi

Model fields don’t have a minimum length. This is a form-only property. I think you should be able to get away with changing just form fields to add this minimum length through a `MinLengthValidator`.

Thanks,

Adam

---

<div class="post-metadata">

**Author:** ![eqmunir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/eqmunir/32/1073_2.png) [@eqmunir](https://forum.djangoproject.com/u/eqmunir)\
**Post date:** [May 23, 2020, 10:48pm UTC](https://forum.djangoproject.com/t/django-change-user-models-minimum-length/2595/3 "2020-05-23T22:48:25Z")

</div>

Hi. So your saying you don’t have to specify any min/max length validators on the model fields, but the forms ?

What about if someone bypasses the form, and tries to insert data straight into the database, and they are able to specify a user with username of length 1 for example.

Does Django have anything in place to cover this possibility?

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [May 23, 2020, 11:13pm UTC](https://forum.djangoproject.com/t/django-change-user-models-minimum-length/2595/4 "2020-05-23T23:13:56Z")

</div>

You can add a `CheckConstraint` for the field in that case, for example, here’s a Book with a title whose length is at least 1 character:

```auto
from django.db import models
from django.db.models.functions import Length

# Register __length transform as per Length documentation
models.CharField.register_lookup(Length)

class Book(models.Model):
    title = models.CharField(max_length=200)

    class Meta:
        constraints = [
            models.CheckConstraint(
                check=models.Q(title __length__ gte=1),
                name="%(app_label)s_%(class)s_title_length",
            )
        ]

```

---

<div class="post-metadata">

**Author:** ![eqmunir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/eqmunir/32/1073_2.png) [@eqmunir](https://forum.djangoproject.com/u/eqmunir)\
**Post date:** [May 23, 2020, 11:17pm UTC](https://forum.djangoproject.com/t/django-change-user-models-minimum-length/2595/5 "2020-05-23T23:17:51Z")

</div>

Great, thank you, this helps a lot.

---

<div class="post-metadata">

**Author:** ![guzmanojero](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/guzmanojero/32/1675_2.png) [@guzmanojero](https://forum.djangoproject.com/u/guzmanojero)\
**Post date:** [July 16, 2020, 6:31pm UTC](https://forum.djangoproject.com/t/django-change-user-models-minimum-length/2595/6 "2020-07-16T18:31:24Z")

</div>

Hi Adam,  
conceptually, what’s the difference between `CheckConstraint` and a `Validator`? What’s the use case for each?  
Sorry if this is off topic.

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [July 16, 2020, 6:57pm UTC](https://forum.djangoproject.com/t/django-change-user-models-minimum-length/2595/7 "2020-07-16T18:57:42Z")

</div>

`CheckConstraint` - in the database  
`Validator` - in forms

---

<div class="post-metadata">

**Author:** ![guzmanojero](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/guzmanojero/32/1675_2.png) [@guzmanojero](https://forum.djangoproject.com/u/guzmanojero)\
**Post date:** [July 16, 2020, 7:54pm UTC](https://forum.djangoproject.com/t/django-change-user-models-minimum-length/2595/8 "2020-07-16T19:54:06Z")

</div>

Quick and effective.  
Thanks!
