Django-Rest-Framework关系&超链接API问题

时间:2014-12-08 22:17:24

标签: python django django-rest-framework

我正在进行django-rest-framework。一切顺利,直到我到了关系&本教程的超链接API部分。在弄乱它之后我现在得到的错误是:

ImproperlyConfigured at /api/users/ "^\.(?P<format>[a-z0-9]+)\.(?P<format>[a-z0-9]+)$" is not a valid regular expression: redefinition of group name u'format' as group2; was group 1

我尝试对此进行一些研究,但似乎找不到任何东西,而且我弄得越多就越错误

继承我的代码:

modules.py

class Home(models.Model):
    user = models.ForeignKey(User)
    #address ect

serializers.py

class UserSerializer(serializers.HyperlinkedModelSerializer):
    username = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='home-detail')

    class Meta:
        model = User
        fields = ('url', 'username', 'home')


class HomeSerializer(serializers.HyperlinkedModelSerializer):
    owner = serializers.Field(source='owner.username')
    highlight = serializers.HyperlinkedIdentityField(view_name='home-highlight', read_only=True, format='html')

    class Meta:
        model = Home
        fields = ('url', 'owner', 'postcode')

api.py

@api_view(('GET',))
def api_root(request, format=None):
    return Response({
        'users': reverse('user-list', request=request, format=format),
        'homes': reverse('home-list', request=request, format=format)
    })


class UserList(generics.ListCreateAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer


class UserDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer


class HomeList(generics.ListCreateAPIView):
    queryset = Home.objects.all()
    serializer_class = HomeSerializer


class HomeDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Home.objects.all()
    serializer_class = HomeSerializer


class HomeHighlight(generics.GenericAPIView):
    queryset = Home.objects.all()
    renderer_classes = (renderers.StaticHTMLRenderer,)

    def get(self, request, *args, **kwargs):
        snippet = self.get_object()
        return Response(snippet.highlighted)

urls.py

urlpatterns = format_suffix_patterns([
    url(r'^$', api.api_root),

    url(r'^users/$',
        api.UserList.as_view(),
        name='user-list'),
    url(r'^users/(?P<pk>[0-9]+)/$',
        api.UserDetail.as_view(),
        name='user-detail'),
    url(r'^home/$',
        api.HomeList.as_view(),
        name='home-list'),
    url(r'^home/(?P<pk>[0-9]+)/$',
        api.HomeDetail.as_view(),
        name='home-detail'),
    url(r'^home/(?P<pk>[0-9]+)/highlight/$',
        api.HomeHighlight.as_view(),
        name='home-highlight')
])

urlpatterns += [
    url(r'^api-auth/', include('rest_framework.urls',
                               namespace='rest_framework')),
]

urlpatterns = format_suffix_patterns(urlpatterns)

2 个答案:

答案 0 :(得分:30)

您正在调用format_suffix_patterns两次,因此Django不知道如何解析该网址,因为有两个format组。

您不应该需要第一个电话,因为第二个电话会为您处理(并允许TokenAuthentication仍然有后缀)。

答案 1 :(得分:-1)

在urls.py文件中将router.DefaultRouter()更改为router.SimpleRouter()