I am trying to create an API for custom user model to add a new user using generic views.
I am getting an error and not able to identify the root cause of it. Though the user gets added successfully. But I still recieves a HTTP 500 error in my response.
ImproperlyConfigured at /api/new_user Could not resolve URL for hyperlinked relationship using view name "user-detail". You may have failed to include the related model in your API, or incorrectly configured the
lookup_field
attribute on this field.
My apiView class is as follows.
class CreateUserView(CreateAPIView):
"""
This view creates a new user
"""
serializer_class = UserSerializer
def perform_create(self, serializer: UserSerializer):
try:
serializer.save(
email=serializer._validated_data['email'],
first_name=serializer._validated_data['first_name'],
last_name=serializer._validated_data['last_name'],
password=make_password(serializer._validated_data['password'])
)
except Exception as exception:
return JsonResponse({
'success': False,
'error': 'Error occured in registering user!'
},
status=500)
return JsonResponse({'success': True})
and my serializer class is as below.
class UserSerializer(serializers.ModelSerializer):
password = serializers.CharField(max_length=128, min_length=8,
style={'input_type': 'password'}, write_only=True)
class Meta:
model = User
fields = ('url', 'id', 'email', 'first_name', 'last_name', 'password',)
and my url patterns file is as follows
urlpatterns = [
path('api/new_user', CreateUserView.as_view()),
path('api/login/', obtain_auth_token, name='api-token-auth')
]
Please help me resolving this.