Reputation: 3193
I am working with Django users, and I've hashed the passwords when I create an user with Django REST Framework
and I override the create
and update
methods on my serializer to hash my passwords users
class UserSerializer(serializers.ModelSerializer):
#username = models.CharField()
def create(self, validated_data):
password = validated_data.pop('password', None)
instance = self.Meta.model(**validated_data)
if password is not None:
instance.set_password(password)
instance.save()
return instance
def update(self, instance, validated_data):
for attr, value in validated_data.items():
if attr == 'password':
instance.set_password(value)
else:
setattr(instance, attr, value)
instance.save()
return instance
class Meta:
model = User
fields = ('url', 'username', 'password', 'first_name','last_name',
'age', 'sex', 'photo', 'email', 'is_player', 'team',
'position', 'is_staff', 'is_active', 'is_superuser',
'is_player', 'weight', 'height', 'nickname',
'number_matches', 'accomplished_matches',
'time_available', 'leg_profile', 'number_shirt_preferred',
'team_support', 'player_preferred', 'last_login',
)
My views.py is this:
class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
filter_fields = ('username', 'is_player', 'first_name', 'last_name', 'team' , 'email', )
My REST_FRAMEWORK settings are:
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.AllowAny',
),
'PAGE_SIZE': 10
}
The inconvenient that I have is that when I create and user via rest framework, the password is hashed, but I cannot sign in or login via rest authentication and Django admin too.
How to can I hash my passwords and sign in via Djago REST FRamework too?
Best Regards
Upvotes: 2
Views: 1042
Reputation: 354
Add rest framework authentication setting with following also
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
)
Ref http://www.django-rest-framework.org/api-guide/authentication/#sessionauthentication
And for token autentication go through doc http://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication
Upvotes: 1