Y M
Y M

Reputation: 2202

How to use Custom User Model for authentication Djoser - Django

Its not clear from the documentation, but I want to achieve 2 things:

  1. Use my custom user model which inherits from models.Model.

  2. It has a password field which stores password using sha1. I need to use model with that password field for authentication.

Since, the tables were used as it is in the project earlier need to use those only.

I need to use any token based authentication.

Can someone please point to the correct direction. I read django-rest-framework documentation, it pointed out to use Djoser for custom user model. But I'm unable to figure this out. How.

Upvotes: 2

Views: 6656

Answers (3)

Ochui Princewill
Ochui Princewill

Reputation: 157

creating a custom user Model inheriting from models.Model is a very bad idea instead use AbstractUser or AbstractBaseUser.

Having created a custom, you have set the AUTH_USER_MODEL to your User model in settings.py.

Then just import the Djoser User Registration Serializer And override it.

from djoser.serializers import UserCreateSerializer as BaseUserRegistrationSerializer

class UserRegistrationSerializer(BaseUserRegistrationSerializer):
    class Meta(BaseUserRegistrationSerializer.Meta):
        fields = ('city', 'state', 'email', 'name', 'last_name', 'account_address', 'password', )

You can also override other things in the serializer like create and update methods in case if you want to customize it.

And in settings.py tell djoser to use it like so

DJOSER = {
    ...
    'SERIALIZERS': {
         'user_create': 'yourapp.serializer.UserRegistrationSerializer'
    }
    ...
}

Upvotes: 4

Kamil Niski
Kamil Niski

Reputation: 4765

You should not create custom user inheriting from models.Model instead use AbstractUser

Here are relevant docs.

If you follow the Django docs Djoser should work without a problem.

Djoser and many other auth related packages use functionality provided by AbstractUser

Upvotes: 1

a_k_v
a_k_v

Reputation: 1608

You can use JWT token authentication. http://getblimp.github.io/django-rest-framework-jwt/. If you set AUTH_USER_MODEL in your setting file to your custom user model then it will validate user. Or you can write custom methods for authentication.

Upvotes: 0

Related Questions