Django case insensitive login, mixed case username

I'm trying to make registration insensitive for a Django app without changing the original username. According to my research, the best approach is to create a bound scope to store the lowercase username when registering. Not reinventing the user model is a simple solution, right? My dilemma: how do you take data from one model, change it and store it in another model when you create an object?

Here's what's in models.py :

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    lc_n = User.username
    lc_n = lc_n.lower()
    lowercase_name = models.CharField(max_length=30, default=lc_n)

      

The python manage.py makemigrations

following error appears on startup :

AttributeError: type object 'User' has no attribute 'username'

The user has this attribute, but it cannot be accessed.

Please forgive me as this should contain some simple fundamental flaws. Any support that can be offered would be greatly appreciated.

+3


source to share


1 answer


You do not need any additional models to implement a case insensitive registration case. Django supports case insensitive filtering using the operator iexact

:



user = User.objects.get(username__iexact=name)

      

+8


source







All Articles