我使用django rest framework有以下代码。
urls.py
urlpatterns = [
url(r'^portfolio1/', include('portfolio1.urls')),
url(r'^admin/', admin.site.urls),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))]
portfolio1 / urls.py
app_name = 'portfolio1'
urlpatterns = [
url(r'^$', \
views.api_root),
url(r'^users/$', \
views.UserList.as_view(), \
name='user-list'),
url(r'^users/(?P<pk>[-\d]+)/$', \
views.UserDetail.as_view(), \
name='user-detail'),
]
urlpatterns = format_suffix_patterns(urlpatterns)
portfolio1 / serializers.py
from django.contrib.auth.models import User as AuthUser
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = AuthUser
fields = ('id', 'username', 'url')
portfolio1 / views.py
from django.contrib.auth.models import User as AuthUser
class UserList(generics.ListAPIView):
queryset = AuthUser.objects.all()
serializer_class = UserSerializer
class UserDetail(generics.RetrieveAPIView):
queryset = AuthUser.objects.all()
serializer_class = UserSerializer
@api_view(('GET',))
@permission_classes((permissions.AllowAny, ))
def api_root(request, format=None):
return Response({
'users': reverse('user-list', request=request, format=format)
})
我收到以下错误。
NotperlyConfigured:无法使用视图名称“user-detail”解析超链接关系的URL。您可能未能在API中包含相关模型,或者在此字段上错误地配置了
lookup_field
属性。
我知道这个错误意味着什么,但不知道为什么我会得到它。是否必须对django rest framework(https://github.com/tomchristie/django-rest-framework/issues/2711)的版本进行任何操作?我已经确保我没有犯下与HyperlinkedIdentityField returning error only on User model相同的错误。
答案 0 :(得分:0)
删除function groupStrings($input)
{
$dict = \array_reduce($input, function(array $out, $in) {
//retrieve the unique characters of each word
$key = \array_unique(\str_split($in));
//sort alphabetically A-Z (ignores case)
\natcasesort($key);
//create an associative index from the key
$key = \implode('', $key);
if(!\array_key_exists($key, $out)) {
$out[$key] = [];
}
$out[$key][] = $in;
return $out;
}, []);
return \implode(',', \array_map(function($word) {
return \implode(',', $word);
}, $dict));
}
$input = ['student', 'students', 'dog', 'studentssess', 'god', 'cat'];
echo groupStrings($input);
中的app_name = 'portfolio1'
。