Search code examples
djangodjango-modelsdjango-viewspython-3.6django-users

My app is not recognising AUTH_USER_MODEL as the default User model


I have created a custom User model. My model is as follow:

class User(AbstractUser):
    ...
    Name = models.CharField(max_length=128)
    ...

When I try to make a post request to this model through my app, in the views.py file I have code that goes like this:

 def post(self,request):
        name = request.POST['name']
        email = request.POST['email']
        password = request.POST['password']
        age = request.POST['age']
        number = request.POST['number']
        gender = request.POST['gender']
        organisation = request.POST['organisation']
        designation = request.POST['designation']
        
        u = User(
             Name = name,
        ...

I get an error that goes like this:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/views/generic/base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/adhishwars/Desktop/simulations/beergame/views/instructor.py", line 37, in post
    u = User(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/db/models/base.py", line 500, in __init__
    raise TypeError("%s() got an unexpected keyword argument '%s'" % (cls.__name__, kwarg))
TypeError: User() got an unexpected keyword argument 'Name'

I made sure I included AUTH_USER_MODEL = 'app.User' in the settings.py file at the beginning of the project before I applied any migrations. Yet I still get this error.

Then, I deleted my migrations folder, deleted my db.sqlite3 file, then ran python3 manage.py makemigrations app, python3 manage.py migrate . I still get the same error. My app is not recognising my custom User model as the default User model.

Any suggestions?


Solution

  • Found the error guys. In my views.py file I had imported the default Django User model.

    from django.contrib.auth.models import User
    

    This caused Django to use its own default User model though I had used my custom User model. As soon as I removed that line it worked.

    EDIT: For some people it may still not work. I suggest using the following code in their views file:

    from django.contrib.auth import get_user_model
    
    User = get_user_model()