Home > Software engineering >  Django user model override
Django user model override

Time:10-25

I'm not advanced in Django and would like to know how and if it's possible to do it.

I need to override Django's user model, and change the fields "username" to "sr_usuario" and "password" to "sr_password", but I would like to continue using all Django's default authentication scheme and permissions. I want not only to change the description in the database or a label, I want to change the name of the field in the model, for when for example I needed to make a query, I would use User.objects.filter(sr_usuario="user_name") and everything would work usually.

It's possible? I couldn't find anything in the documentation or on forums I've searched.

Thank you very much in advance!

CodePudding user response:

Yes, if you want to override the User Model you need to add to your settings:

AUTH_USER_MODEL = "<THE PATH OF THE USER MODEL>"

And in that model extend it using the AbstractUser

Django Documentation here: https://docs.djangoproject.com/en/4.1/topics/auth/customizing/#using-a-custom-user-model-when-starting-a-project

CodePudding user response:

This is not possible, as all the django code is strictly coupled to the fields in the user.

You can, however, create a custom authentication backend and use a custom user model with the field names mentioned. But this would require a lot of coding.

  • Related