简体   繁体   中英

Could not resolve URL for hyperlinked relationship

see many post about the similar issue, either not very clear, or I can't understand, so post my problem here again.

I have many other 'xxxList' and 'xxxDetail', all working fine, except this always with exception:

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

Model :

class UserInfo(models.Model):
    # extend Django user object
    user = models.ForeignKey(User, related_name='parkingUser')

Serializer:

class UserInfoSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = UserInfo

class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'username', 'is_staff', 'is_active', 'date_joined')

View:

class UserInfoList(generics.ListCreateAPIView):
    queryset = UserInfo.objects.all()
    serializer_class = UserInfoSerializer
    permission_classes = (permissions.IsAuthenticated,)

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)

class UserInfoDetail(generics.RetrieveAPIView):
    queryset = UserInfo.objects.all()
    serializer_class = UserInfoSerializer
    permission_classes = (permissions.IsAuthenticated, )    

class UserList(generics.ListCreateAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    permission_classes = (permissions.IsAuthenticated, )

class UserDetail(generics.RetrieveAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    permission_classes = (permissions.IsAuthenticated, IsSuperUsersGroupOrDeny)

App Url:

 url(r'^usersInfo/$', views.UserInfoList.as_view(), name='userInfo-list'),
 url(r'^usersInfo/(?P<pk>[0-9]+)/$', views.UserInfoDetail.as_view(), name='userInfo-detail'),
 url(r'^users/$', views.UserList.as_view(), name='user-list'),
 url(r'^users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view(), name='user-detail'),

I nearly removed all logic code for testing and it can't be simpler anymore, still the same exception, the weird thing is if I tested the 'userInfoDetail' with standard url in browser like:

http://rest.myDomain.xxx:8090/usersInfo/replaceWithIntegerNumber/

some of them return the same error, some of them return

{ "detail": "Not found." }

what's the problem here?

I may be wrong, but the problem seems to be the capital "I" in your usersInfo URL. Try name='userinfo-list' and name='userinfo-detail' instead of name='userInfo-list' and name='userInfo-detail' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM