I am getting the common DRF hyperlinked model-detail error, and I'd like to understand what it means so I stop getting it.
The view is as such:
import json
import time
from django.http import JsonResponse
from django.contrib.auth.models import User
from rest_framework import serializers, viewsets
from utils.general import unpack_request_body
from .models import Restaurant
#------------------------------------------------------------------------------------
class RestaurantSerializer(serializers.HyperlinkedModelSerializer):
# answered by bovenson:
# https://stackoverflow.com/questions/20550598/django-rest-framework-could-not-resolve-url-for-hyperlinked-relationship-using
# what just happened here? idk what HyperlinkedIdentityField does
url = serializers.HyperlinkedIdentityField(view_name="restaurant:restaurant-detail")
class Meta:
model = Restaurant
fields = '__all__'
class RestaurantViewSet(viewsets.ModelViewSet):
queryset = Restaurant.objects.all()
serializer_class = RestaurantSerializer
#------------------------------------------------------------------------------------
def find_restaurant_owned_by_a_username(username):
try:
user = User.objects.get(username=username)
except:
return None
restaurants_owned = user.restaurants.all()
if restaurants_owned:
return restaurants_owned[0]
else:
return None
def restaurants_owned_by_this_username(request):
""" for now only returns one restaurant """
# TODO: will need to support multiple restaurants owned by one user in the future
if request.method == "POST":
body = unpack_request_body(request)
username = body['username']
restaurant_owned = find_restaurant_owned_by_a_username(username)
if restaurant_owned:
serializer = RestaurantSerializer(restaurant_owned, context={'request': request})
return JsonResponse({'result': serializer.data})
else:
return JsonResponse({'result': None})
else:
error_message = "This method only responds to POST"
print(error_message)
return JsonResponse({'error': error_message})
urls:
urlpatterns = [
...
url(r'^api/restaurants-owned', csrf_exempt(views.restaurants_owned_by_this_username), name="restaurants-owned"),
]
models.py:
from django.contrib.auth.models import User
class Restaurant(models.Model):
name = models.CharField(max_length=250, null=False, blank=False)
phone = models.CharField(max_length=12)
owner = models.ForeignKey(User, models.SET_NULL, related_name="restaurants", null=True, blank=True)
address1 = models.CharField(max_length=250, null=False, blank=False)
address2 = models.CharField(max_length=250, null=True, blank=True)
city = models.CharField(max_length=250, null=False, blank=False)
state = models.CharField(max_length=2, null=False, blank=False)
zip_code = models.CharField(max_length=5, null=False, blank=False)
lat = models.DecimalField(max_digits=10, decimal_places=7, null=True, blank=True)
lng = models.DecimalField(max_digits=10, decimal_places=7, null=True, blank=True)
I now hit the endpoint like POST http://localhost:8000/api/restaurants-owned/ {"username": "codyc4321"}
I get
django.core.exceptions.ImproperlyConfigured: 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.
I have been looking up this error message, and after reading the ways to "solve" it, still don't understand it. It doesn't seem people are explaining what it means. I don't want a user-detail view, I only included user as a foreign key for who owns the restaurant. As the views.py
shows, I solved this error once and didn't understand how it worked.
How can I stop this error message in the future and what is Django trying to say with it?