[Django]-How to override max length of username in Django by creating a custom model

4πŸ‘

βœ…

If you want it to be more flexible inherit from AbstractBaseUser instead, you will be able to specify your own username field that way, e.g.

class User(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )

    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    date_joined = models.DateTimeField(
        verbose_name=_('date created'),
        auto_created=True,
        default=timezone.now
    )

    objects = UserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

https://docs.djangoproject.com/en/1.7/topics/auth/customizing/#specifying-a-custom-user-model

8πŸ‘

Dont try to override username field. Just

class CustomUser(AbstractUser, BaseModel):

       #here goes other fields

CustomUser._meta.get_field('username').max_length = 70 

But I highly recommend to inherit from AbstractBaseUser instead.

πŸ‘€levi

0πŸ‘

As I understood you can’t override any Django ORM model’s attribute, nor from AbstractUser, nor from PermissionsMixin classes. So, If you have your own approach to Permissions, Groups(Roles), even if you want another attribute name than user_permissions or groups, then you should inherit from AbstractBaseUser, doing a lot of copy-paste from AbstractUser and inherit your Permission and Group models from django.db.models.Model class as @levi and @Hedde van der Heide suggested.

πŸ‘€Alex-Bogdanov

Leave a comment