django rest framework hyperlinked relationship error

Here’s an error that’s all too easy to stumble on if you are just hacking your way into an API using Django REST Framework

Could not resolve URL for hyperlinked relationship using view name “model-detail”. You may have failed to include the related model in your API, or incorrectly configured the lookup_field attribute on this field

Recreating the error

I’ll assume you have, at least, a basic Django site up and running. Perhaps you are a little impatient (like me) and you skim the Django REST Framework’s homepage. You add the following in to settings.py:

And then, somehow, you skip he rest of the tutorial and you end up with something like this:

views.py:

urls.py:

Make sure you run your migration or sync your db.
Now you should be able to browse your api with the following url:

Things are looking good, job done? Not so fast……spend a moment inserting a record into the Pet table – create a fixture, write some raw sql, whatever floats your boat. Refresh your api endpoint and you’ll get a glorious error:

“Could not resolve URL for hyperlinked relationship using view name “pet-detail”. You may have failed to include the related model in your API, or incorrectly configured the lookup_field attribute on this field.”

How do you dig yourself out of this one?

Two methods – the first (possibly simplest) is to cut the following out of settings.py (commented out so you don’t skim my post and add it in again):

In actual fact, removing just the following two lines will fix it for you:

The second is to create a serializers.py class like so:

and update your views.py as follows:

and you will be back in the land of the living.

Why does this happen?

Well, the key lies in the Default Model Serializer Class – when it is set to HyperlinkedModelSerializer, the rest framework needs a serializer to work with.

Kvetch

The error message here is really, really unhelpful for a anyone not familiar with the Django Rest Framework.

Could not resolve URL for hyperlinked relationship using view name “pet-detail”

made me start questioning my sanity – I had neither a detail suffix view defined, nor any sort of relationship with another model – both things I thought would be obvious causes of the error.

The added annoyance of the bug on rearing its head when data is populated also irked me somewhat because as part of my bug exploration, I created a new stripped down project (as above) to test the damn thing out. I didn’t include any fixtures in my test project, so initially, it looked like it worked and spent a hunk of time back in my original application looking for bugs in “my” code rather than default settings of the REST framework itself.

Ultimately the lesson is: RTFM? Well yes and no. I do think that projects should have an “at a glance” page, and I erroneously thought the Django REST Framework’s homepage would be it – you know “whack this in your app and you’ll have a basic json response going” type of thing. Of course this is a case where I should have read just a little bit more to save myself a headache.

Aah well, live and learn.

8 Comments

  1. Tom Christie says:

    As of 2.3.6 this situation gets a more verbose error message…

    https://github.com/tomchristie/django-rest-framework/commit/35f99cddc4a098547389fab7d9f397ad442dfff1#L1R403

    ‘Could not resolve URL for hyperlinked relationship using ‘
    ‘view name “%s”. You may have failed to include the related ‘
    ‘model in your API, or incorrectly configured the ‘
    lookup_field attribute on this field.’

    Will happily consider any requested tweaks to the current error messaging,
    tickets here if so… https://github.com/tomchristie/django-rest-framework/issues :)

  2. Tom Christie says:

    Oops I’ve just noticed that the ‘improved’ error message is exactly the message you’ve posted – oh well, clearly it could use some clarification – suggestions welcome.

    • Bevan says:

      Hi Tom – thanks for the comments – suggestions as to the error message – I’m not sure – perhaps the error message is actually correct in its context – and maybe a super simple “getting started guide” for the framework would be good (especially for noobs like me ).

      What I was actually looking for really, was a way to integrate the framework initially to serve up some json for an ajax callback with a view to a more fully featured API later in the project’s lifetime. Mea Culpa – I was in a rush and really just wanted to get it up and running and returning something in 30 minutes that would be superior to any json serialization that I could cook up on my end…….Would love to help with the docs, not sure if I’m qualified though :)

      • Tom Christie says:

        I think the fact that you ran into this and wrote a post on it is sufficient evidence that the error messaging isn’t clear enough.

        > Would love to help with the docs, not sure if I’m qualified though

        It’s the end-user’s perspective that matters, not mine. If anything I’ve got less visibility onto what is or isn’t easy to use or understand.

        I’ve opened a ticket for improving the messaging here: https://github.com/tomchristie/django-rest-framework/issues/977

  3. Alex says:

    This error can also occur if you explicitly specify a url in a serializer that extends HyperlinkedModelSerializer.

  4. Phedoreanu says:

    Excellent! Thanks man! :)

  5. Nikhita says:

    Thank you for this. I was doing exactly the same thing (without knowing exactly what I was getting myself into).

  6. friers says:

    Thank you! This was very helpful in my own attempt to get django rest framework up and running.