close

[Solved] Django Rest Framework – Could not resolve URL for hyperlinked relationship using view name “user-detail”

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Django Rest Framework – Could not resolve URL for hyperlinked relationship using view name “user-detail” in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Django Rest Framework – Could not resolve URL for hyperlinked relationship using view name “user-detail” Error Occurs?

Today I get the following error Django Rest Framework – Could not resolve URL for hyperlinked relationship using view name “user-detail” in python.

How To Solve Django Rest Framework – Could not resolve URL for hyperlinked relationship using view name “user-detail” Error ?

  1. How To Solve Django Rest Framework – Could not resolve URL for hyperlinked relationship using view name “user-detail” Error ?

    To Solve Django Rest Framework – Could not resolve URL for hyperlinked relationship using view name “user-detail” Error I registered the rest framework routers in myapp/urls.py, and then got this error.

  2. Django Rest Framework – Could not resolve URL for hyperlinked relationship using view name “user-detail”

    To Solve Django Rest Framework – Could not resolve URL for hyperlinked relationship using view name “user-detail” Error I registered the rest framework routers in myapp/urls.py, and then got this error.

Solution 1

I came across this error too and solved it as follows:

The reason is I forgot giving “**-detail” (view_name, e.g.: user-detail) a namespace. So, Django Rest Framework could not find that view.

There is one app in my project, suppose that my project name is myproject, and the app name is myapp.

There is two urls.py file, one is myproject/urls.py and the other is myapp/urls.py. I give the app a namespace in myproject/urls.py, just like:

url(r'', include(myapp.urls, namespace="myapp")),

I registered the rest framework routers in myapp/urls.py, and then got this error.

My solution was to provide url with namespace explicitly:

class UserSerializer(serializers.HyperlinkedModelSerializer):
    url = serializers.HyperlinkedIdentityField(view_name="myapp:user-detail")

    class Meta:
        model = User
        fields = ('url', 'username')

And it solved my problem.

Solution 2

Because it’s a HyperlinkedModelSerializer your serializer is trying to resolve the URL for the related User on your Bottle.
As you don’t have the user detail view it can’t do this. Hence the exception.

  1. Would not just registering the UserViewSet with the router solve your issue?
  2. You could define the user field on your BottleSerializer to explicitly use the UserSerializer rather than trying to resolve the URL.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read