Django custom User model throwing SystemCheckError - The field 'username' clashes with the name 'username'

SkullTech

I'm writing a custom User model in Django, inheriting it from AbstractUser as is mentioned in the official Django tutorials. But this throws an error.

django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
redditauth.RedditUser.username: (models.E006) The field 'username' clashes with the field 'username' from model 'redditauth.reddituser'.

Here is the code I wrote for the custom user model.

class RedditUser(AbstractUser):
    username = models.CharField(unique=True, primary_key=True, validators=[validate_reddit_username], max_length=20)
    token = models.CharField(max_length=256)

    USERNAME_FIELD = username
    REQUIRED_FIELDS = ['token']

    def reddit(self):
        with open('secret.json', 'r') as f:
            secret = json.load(f)

        return praw.Reddit(client_id=secret['client_id'], client_secret=secret['client_secret'],
                           refresh_token=self.token, user_agent='Plan-Reddit by /u/SkullTech101')

I have tried renaming it to something other than username, thinking that maybe there was already field named username in AbstractUser, but that didn't solve the problem.

SkullTech

As @Alasdair pointed out, I had to use a string, not a variable, when setting the value of USERNAME_FIELD.

So in my case it was

USERNAME_FIELD = 'username'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to change the field of username to user_name in to django custom user model?

Multiple USERNAME_FIELD in django user model

Login user for a custom user model with email as username field

Field name `username` is not valid for model

Field name user_username is not valid for model Profile

Compare Django model field and request.user.username in HTML

Django model owner field reading the log in user as None instead of the username

How do I add username of logged in user to model field in django

Django 1.10 custom User 'is_superuser' clashes with the field 'is_superuser' from model

Django get user by generic USERNAME_FIELD

Custom User Model Django TypeError: create_user() missing 1 required positional argument: 'username'

FieldDoesNotExist error when using Dj-Rest-Auth registration with a custom user model that does not have a username field

Django custom user model gives error: TypeError: create_superuser() got an unexpected keyword argument 'username'

DJANGO custom user model: create_superuser() missing 1 required positional argument: 'username'

Why is it throwing an Django- Reverse query name SystemCheckError?

How to associate a username from the User model to another model in Django?

Populate Django username field with generated username

Django user creation return "Key 'username' not found in 'CustomUserForm'" for custom user

Display username in template with custom user model as welcome message

How to validate custom user model username to prevent blank spaces

authenticate(request, username=username, password =pswd) returns None for custom user model

My devise User model custom field username always nil. I have tried all the other solutions on stack overflow

Putting username of logged in user as label in django form field

Remove/hiding username field in django admin edit user form

Django Rest Framework: Serialize User ManyToMany field by username

django.core.exceptions.FieldDoesNotExist: User has no field named 'username'

Django custom auth model registration form returns unique username error

Django - Take username of logged user as default value to another model

How can I get a username from django User Model