我收到了常见的DRF超链接模型详细信息错误,我想了解它的含义,所以我不再理解它。
观点如下:
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})
网址:
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)
我现在点击端点,如POST http://localhost:8000/api/restaurants-owned/ {"username": "codyc4321"}
我得到了
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.
我一直在查找此错误消息,并在阅读完"解决"它,仍然不明白。人们似乎并没有解释它的含义。我不想要用户详细信息视图,我只将用户作为拥有该餐厅的外键。正如views.py
所示,我解决了这个错误一次并且不了解它是如何工作的。
如何在将来停止此错误消息以及Django试图用它来说什么?