Django-rest-framework命名空间反向失败

时间:2016-05-27 15:17:05

标签: django django-rest-framework

我正在使用Django 1.8.9和Python 3.4的django-rest-framework 3.3.2。 我在DRF注册了一个模型位置。我的所有测试都通过了我今天决定在我的API中添加命名空间,但我无法使其工作:我的测试失败。

这是代码和测试:

urls.py (项目级别)

from myproject.apps.core.api import router as core_api_router

urlpatterns = patterns('',
                    # API
                    url(r'^api/', include(core_api_router.urls, namespace='api')),
                    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
                    url(r'^api-docs/', include('rest_framework_swagger.urls', namespace='api_docs')),
                   )

api.y

class LocationReadOnlySerializer(DynamicModelSerializerMixin, serializers.HyperlinkedModelSerializer):
    status = LocationStatusSerializer()

    class Meta:
        model = models.Location
        fields = ('id', 'href', 'sid', 'code', 'name', 'short_name', 'country',
              'state', 'city', 'street', 'postcode', 'floor',
              'room', 'status', 'parent'
              'purpose', 'created')
        ordering_fields = '__all__'
        ordering = ('name',)


    class LocationViewSet(viewsets.ModelViewSet):
        queryset = models.Location.objects.all().select_related('status')

        def get_serializer_class(self, *args, **kwargs):
            if self.request.method in ['PATCH', 'POST', 'PUT']:
                return LocationWriteOnlySerializer
            else:
                return LocationReadOnlySerializer


router.register('core/locations', LocationViewSet, 'api:location')

tests.py

def test_api_update(self):
    entry = self.Meta.factory.create()

    url = reverse('api:' + self.api_model_url + '-detail', args=[str(entry.id)])

    data = {'sid': 'Modified location'}

    # Check that an entry can be altered by an administrator via the API
    self.api_client.login(username='admin-user', password='admin')
    response = self.api_client.patch(url, data, format='json')
    content = self.parse_json_response(response, Status.HTTP_200_OK)
    self.assertEqual(content['sid'], 'Modified location')

API命名空间版本控制已停用(我在settings.py中注释了该行)

# API Versioning
#'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.NamespaceVersioning',
#'DEFAULT_VERSION': '1.0',
#'ALLOWED_VERSIONS': ('v1', ),

因此,当我运行上述测试时出现以下错误:

django.core.urlresolvers.NoReverseMatch: Reverse for 'location-detail' with arguments '('fc5d358d-5658-4a82-bb57-b5244e6b3602',)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

你看到我错过了吗?

提前致谢!

0 个答案:

没有答案