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

前端 未结 17 1367
感动是毒
感动是毒 2020-11-28 03:11

I am building a project in Django Rest Framework where users can login to view their wine cellar. My ModelViewSets were working just fine and all of a sudden I get this frus

17条回答
  •  粉色の甜心
    2020-11-28 03:48

    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. See the serializer docs on dealing with nested objects for that.

提交回复
热议问题