I am building a project in Django Rest Framework to interact with my React app, enabling users to signup and create a profile.
However, when I run python manage.py runserver and click on the url appearing on API Root Page I get the following error message:
ImproperlyConfigured at /profiles/
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.
Here is my models.py code:
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
name = models.CharField(max_length=100, blank=True)
location = models.CharField(max_length=100, blank=True)
email = models.EmailField(max_length=150)
signup_confirmation = models.BooleanField(default=False)
def __str__(self):
return self.user.username
@receiver(post_save, sender=User)
def update_profile_signal(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
instance.profile.save()
My serializers.py:
from rest_framework import serializers
from .models import Profile
class ProfileSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Profile
fields = ('user', 'name', 'location', 'email', 'signup_confirmation')
urls.py:
from django.urls import path, include
from rest_framework import routers
from .import views
router = routers.DefaultRouter()
router.register(r'profiles', views.ProfileViewSet)
urlpatterns = [
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]
and views.py
from rest_framework import viewsets
from .serializers import ProfileSerializer
from .models import Profile
from .forms import SignUpForm
from .tokens import account_activation_token
class ProfileViewSet(viewsets.ModelViewSet):
queryset = Profile.objects.all().order_by('name')
serializer_class = ProfileSerializer
Any idea of why I am getting this error