问题:Django Rest Framework-无法使用视图名称“ user-detail”解析超链接关系的URL

我正在Django Rest Framework中建立一个项目,用户可以登录查看其酒窖。我的ModelViewSets工作正常,突然我收到了一个令人沮丧的错误:

无法使用视图名称“用户详细信息”解析超链接关系的URL。您可能无法在API中包含相关模型,或者lookup_field在此字段上配置了错误的属性。

追溯显示:

    [12/Dec/2013 18:35:29] "GET /bottles/ HTTP/1.1" 500 76677
Internal Server Error: /bottles/
Traceback (most recent call last):
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/rest_framework/viewsets.py", line 78, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 57, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/rest_framework/views.py", line 399, in dispatch
    response = self.handle_exception(exc)
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/rest_framework/views.py", line 396, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/rest_framework/mixins.py", line 96, in list
    return Response(serializer.data)
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/rest_framework/serializers.py", line 535, in data
    self._data = [self.to_native(item) for item in obj]
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/rest_framework/serializers.py", line 325, in to_native
    value = field.field_to_native(obj, field_name)
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/rest_framework/relations.py", line 153, in field_to_native
    return self.to_native(value)
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/rest_framework/relations.py", line 452, in to_native
    raise Exception(msg % view_name)
Exception: Could not resolve URL for hyperlinked relationship using view 
name "user-detail". You may have failed to include the related model in 
your API, or incorrectly configured the `lookup_field` attribute on this 
field.

我有一个自定义的电子邮件用户模型,并且models.py中的瓶子模型是:

class Bottle(models.Model):    
      wine = models.ForeignKey(Wine, null=False)
      user = models.ForeignKey(User, null=False, related_name='bottles')

我的序列化器:

class BottleSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Bottle
        fields = ('url', 'wine', 'user')

class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('email', 'first_name', 'last_name', 'password', 'is_superuser')

我的看法:

class BottleViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows bottles to be viewed or edited.
    """
    queryset = Bottle.objects.all()
    serializer_class = BottleSerializer

class UserViewSet(ListCreateAPIView):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = User.objects.all()
    serializer_class = UserSerializer

最后是URL:

router = routers.DefaultRouter()
router.register(r'bottles', views.BottleViewSet, base_name='bottles')

urlpatterns = patterns('',
    url(r'^', include(router.urls)),
    # ...

我没有用户详细信息视图,也看不到此问题可能来自何处。有任何想法吗?

谢谢

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 frustrating error:

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

The traceback shows:

    [12/Dec/2013 18:35:29] "GET /bottles/ HTTP/1.1" 500 76677
Internal Server Error: /bottles/
Traceback (most recent call last):
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/rest_framework/viewsets.py", line 78, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 57, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/rest_framework/views.py", line 399, in dispatch
    response = self.handle_exception(exc)
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/rest_framework/views.py", line 396, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/rest_framework/mixins.py", line 96, in list
    return Response(serializer.data)
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/rest_framework/serializers.py", line 535, in data
    self._data = [self.to_native(item) for item in obj]
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/rest_framework/serializers.py", line 325, in to_native
    value = field.field_to_native(obj, field_name)
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/rest_framework/relations.py", line 153, in field_to_native
    return self.to_native(value)
  File "/Users/bpipat/.virtualenvs/usertest2/lib/python2.7/site-packages/rest_framework/relations.py", line 452, in to_native
    raise Exception(msg % view_name)
Exception: Could not resolve URL for hyperlinked relationship using view 
name "user-detail". You may have failed to include the related model in 
your API, or incorrectly configured the `lookup_field` attribute on this 
field.

I have a custom email user model and the bottle model in models.py is:

class Bottle(models.Model):    
      wine = models.ForeignKey(Wine, null=False)
      user = models.ForeignKey(User, null=False, related_name='bottles')

My serializers:

class BottleSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Bottle
        fields = ('url', 'wine', 'user')

class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('email', 'first_name', 'last_name', 'password', 'is_superuser')

My views:

class BottleViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows bottles to be viewed or edited.
    """
    queryset = Bottle.objects.all()
    serializer_class = BottleSerializer

class UserViewSet(ListCreateAPIView):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = User.objects.all()
    serializer_class = UserSerializer

and finally the url:

router = routers.DefaultRouter()
router.register(r'bottles', views.BottleViewSet, base_name='bottles')

urlpatterns = patterns('',
    url(r'^', include(router.urls)),
    # ...

I don’t have a user detail view and I don’t see where this issue could come from. Any ideas?

Thanks


回答 0

由于是HyperlinkedModelSerializer序列化程序,因此您的序列化程序正在尝试解析与User上相关的URL Bottle
由于您没有用户详细信息视图,因此无法执行此操作。因此,exceptions。

  1. 不仅仅是UserViewSet在路由器上注册可以解决您的问题吗?
  2. 您可以在自己的用户字段上定义BottleSerializer以显式使用,UserSerializer而不是尝试解析URL。请参阅有关处理嵌套对象序列化器文档

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.

回答 1

我也遇到了此错误,并按以下方式解决了该错误:

原因是我忘记给“ **-detail”(view_name,例如:user-detail)命名空间。因此,Django Rest Framework无法找到该视图。

我的项目中有一个应用程序,假设我的项目名称为myproject,而应用程序名称为myapp

有两个urls.py文件,一个是myproject/urls.py,另一个是myapp/urls.py。我在中为应用提供了一个命名空间myproject/urls.py,就像:

url(r'', include(myapp.urls, namespace="myapp")),

我在中注册了其余框架路由器myapp/urls.py,然后出现此错误。

我的解决方案是显式地为URL提供命名空间:

class UserSerializer(serializers.HyperlinkedModelSerializer):
    url = serializers.HyperlinkedIdentityField(view_name="myapp:user-detail")

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

它解决了我的问题。

I came across this error too and solved it as follows:

The reason is I forgot giving “**-detail” (view_name, e.g.: user-detail) a namespace. So, Django Rest Framework could not find that view.

There is one app in my project, suppose that my project name is myproject, and the app name is myapp.

There is two urls.py file, one is myproject/urls.py and the other is myapp/urls.py. I give the app a namespace in myproject/urls.py, just like:

url(r'', include(myapp.urls, namespace="myapp")),

I registered the rest framework routers in myapp/urls.py, and then got this error.

My solution was to provide url with namespace explicitly:

class UserSerializer(serializers.HyperlinkedModelSerializer):
    url = serializers.HyperlinkedIdentityField(view_name="myapp:user-detail")

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

And it solved my problem.


回答 2

也许有人可以看看这个:http : //www.django-rest-framework.org/api-guide/routers/

如果将命名空间与超链接的序列化程序一起使用,则还需要确保序列化程序上的所有view_name参数都能正确反映命名空间。例如:

urlpatterns = [
    url(r'^forgot-password/$', ForgotPasswordFormView.as_view()),
    url(r'^api/', include(router.urls, namespace='api')),
]

您需要包含一个参数,例如view_name='api:user-detail'超链接到用户详细信息视图的序列化程序字段的参数。

class UserSerializer(serializers.HyperlinkedModelSerializer):
    url = serializers.HyperlinkedIdentityField(view_name="api:user-detail")

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

Maybe someone can have a look at this : http://www.django-rest-framework.org/api-guide/routers/

If using namespacing with hyperlinked serializers you’ll also need to ensure that any view_name parameters on the serializers correctly reflect the namespace. For example:

urlpatterns = [
    url(r'^forgot-password/$', ForgotPasswordFormView.as_view()),
    url(r'^api/', include(router.urls, namespace='api')),
]

you’d need to include a parameter such as view_name='api:user-detail' for serializer fields hyperlinked to the user detail view.

class UserSerializer(serializers.HyperlinkedModelSerializer):
    url = serializers.HyperlinkedIdentityField(view_name="api:user-detail")

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

回答 3

导致此错误的另一个令人讨厌的错误是在urls.py中不必要地定义了base_name。例如:

router.register(r'{pathname}', views.{ViewName}ViewSet, base_name='pathname')

这将导致上述错误。在那里获取该base_name并返回到正常工作的API。下面的代码将修复该错误。万岁!

router.register(r'{pathname}', views.{ViewName}ViewSet)

但是,您可能不仅随便添加了base_name,还因为您为View定义了自定义def get_queryset(),所以Django要求您添加base_name。在这种情况下,您需要为相关的序列化程序显式定义“ url”作为HyperlinkedIdentityField。注意,我们在抛出错误的视图的SERIALIZER上定义了此HyperlinkedIdentityField。如果我的错误是“无法使用视图名称“ study-detail”解析超链接关系的URL。您可能无法在API中包含相关模型,或者lookup_field在此字段上配置了错误的属性。” 我可以使用以下代码修复此问题。

我的ModelViewSet(自定义get_queryset是为什么我必须首先将base_name添加到router.register()的原因):

class StudyViewSet(viewsets.ModelViewSet):
    serializer_class = StudySerializer

    '''custom get_queryset'''
    def get_queryset(self):
        queryset = Study.objects.all()
        return queryset

我在urls.py中为此ModelViewSet注册的路由器:

router.register(r'studies', views.StudyViewSet, base_name='studies')

钱在哪里!然后我可以这样解决:

class StudySerializer(serializers.HyperlinkedModelSerializer):
    url = serializers.HyperlinkedIdentityField(view_name="studies-detail")
    class Meta:
        model = Study
        fields = ('url', 'name', 'active', 'created',
              'time_zone', 'user', 'surveys')

是的 您必须自己明确定义此HyperlinkedIdentityField才能起作用。而且,您需要确保view_nameHyperlinkedIdentityField上的定义与base_nameurls.py中的定义相同,并在其后添加“ -detail”。

Another nasty mistake that causes this error is having the base_name unnecessarily defined in your urls.py. For example:

router.register(r'{pathname}', views.{ViewName}ViewSet, base_name='pathname')

This will cause the error noted above. Get that base_name outta there and get back to a working API. The code below would fix the error. Hooray!

router.register(r'{pathname}', views.{ViewName}ViewSet)

However, you probably didn’t just arbitrarily add the base_name, you might have done it because you defined a custom def get_queryset() for the View and so Django mandates that you add the base_name. In this case you’ll need to explicitly define the ‘url’ as a HyperlinkedIdentityField for the serializer in question. Notice we are defining this HyperlinkedIdentityField ON THE SERIALIZER of the view that is throwing the error. If my error were “Could not resolve URL for hyperlinked relationship using view name “study-detail”. You may have failed to include the related model in your API, or incorrectly configured the lookup_field attribute on this field.” I could fix this with the following code.

My ModelViewSet (the custom get_queryset is why I had to add the base_name to the router.register() in the first place):

class StudyViewSet(viewsets.ModelViewSet):
    serializer_class = StudySerializer

    '''custom get_queryset'''
    def get_queryset(self):
        queryset = Study.objects.all()
        return queryset

My router registration for this ModelViewSet in urls.py:

router.register(r'studies', views.StudyViewSet, base_name='studies')

AND HERE’S WHERE THE MONEY IS! Then I could solve it like so:

class StudySerializer(serializers.HyperlinkedModelSerializer):
    url = serializers.HyperlinkedIdentityField(view_name="studies-detail")
    class Meta:
        model = Study
        fields = ('url', 'name', 'active', 'created',
              'time_zone', 'user', 'surveys')

Yep. You have to explicitly define this HyperlinkedIdentityField on itself for it to work. And you need to make sure that the view_name defined on the HyperlinkedIdentityField is the same as you defined on the base_name in urls.py with a ‘-detail’ added after it.


回答 4

该代码也应该起作用。

class BottleSerializer(serializers.HyperlinkedModelSerializer):

  user = UserSerializer()

  class Meta:
    model = Bottle
    fields = ('url', 'wine', 'user')

This code should work, too.

class BottleSerializer(serializers.HyperlinkedModelSerializer):

  user = UserSerializer()

  class Meta:
    model = Bottle
    fields = ('url', 'wine', 'user')

回答 5

将命名空间添加到我的网址后,我遇到了此错误

 url('api/v2/', include('api.urls', namespace='v2')),

并将app_name添加到我的urls.py

我通过在项目的settings.py中为其余框架api指定NamespaceVersioning来解决此问题

REST_FRAMEWORK = {
    'DEFAULT_VERSIONING_CLASS':'rest_framework.versioning.NamespaceVersioning'}

I ran into this error after adding namespace to my url

 url('api/v2/', include('api.urls', namespace='v2')),

and adding app_name to my urls.py

I resolved this by specifying NamespaceVersioning for my rest framework api in settings.py of my project

REST_FRAMEWORK = {
    'DEFAULT_VERSIONING_CLASS':'rest_framework.versioning.NamespaceVersioning'}

回答 6

今天,我遇到了同样的错误,下面的更改可以挽救我。

更改

class BottleSerializer(serializers.HyperlinkedModelSerializer):

至:

 class BottleSerializer(serializers.ModelSerializer):

Today, I got the same error and below changes rescue me.

Change

class BottleSerializer(serializers.HyperlinkedModelSerializer):

to:

 class BottleSerializer(serializers.ModelSerializer):

回答 7

错误相同,但原因不同:

我定义了一个自定义用户模型,没有新的字段:

from django.contrib.auth.models import (AbstractUser)
class CustomUser(AbstractUser):
    """
    custom user, reference below example
    https://github.com/jonathanchu/django-custom-user-example/blob/master/customuser/accounts/models.py

    # original User class has all I need
    # Just add __str__, not rewrite other field
    - id
    - username
    - password
    - email
    - is_active
    - date_joined
    - method, email_user
    """

    def __str__(self):
        return self.username

这是我的视图功能:

from rest_framework import permissions
from rest_framework import viewsets
from .models import (CustomUser)
class UserViewSet(viewsets.ModelViewSet):
    permission_classes = (permissions.AllowAny,)
    serializer_class = UserSerializer

    def get_queryset(self):
        queryset = CustomUser.objects.filter(id=self.request.user.id)
        if self.request.user.is_superuser:
            queryset = CustomUser.objects.all()
        return queryset

由于我没有queryset直接输入UserViewSet,因此必须base_name在注册此视图集时进行设置。这是urls.py文件引起的我的错误消息:

from myapp.views import (UserViewSet)
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register(r'users', UserViewSet, base_name='customuser')  # <--base_name needs to be 'customuser' instead of 'user'

您需要base_name与模型名称相同- customuser

Same Error, but different reason:

I define a custom user model, nothing new field:

from django.contrib.auth.models import (AbstractUser)
class CustomUser(AbstractUser):
    """
    custom user, reference below example
    https://github.com/jonathanchu/django-custom-user-example/blob/master/customuser/accounts/models.py

    # original User class has all I need
    # Just add __str__, not rewrite other field
    - id
    - username
    - password
    - email
    - is_active
    - date_joined
    - method, email_user
    """

    def __str__(self):
        return self.username

This is my view function:

from rest_framework import permissions
from rest_framework import viewsets
from .models import (CustomUser)
class UserViewSet(viewsets.ModelViewSet):
    permission_classes = (permissions.AllowAny,)
    serializer_class = UserSerializer

    def get_queryset(self):
        queryset = CustomUser.objects.filter(id=self.request.user.id)
        if self.request.user.is_superuser:
            queryset = CustomUser.objects.all()
        return queryset

Since I didn’t give queryset directly in UserViewSet, I have to set base_name when I register this viewset. This is where my error message caused by urls.py file:

from myapp.views import (UserViewSet)
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register(r'users', UserViewSet, base_name='customuser')  # <--base_name needs to be 'customuser' instead of 'user'

You need a base_name same as your model name – customuser.


回答 8

如果要扩展GenericViewSetListModelMixin类,并且在列表视图中添加url字段时出现相同的错误,那是因为没有定义详细信息视图。确保您正在扩展RetrieveModelMixin mixin:

class UserViewSet (mixins.ListModelMixin,
                   mixins.RetrieveModelMixin,
                   viewsets.GenericViewSet):

If you’re extending the GenericViewSet and ListModelMixin classes, and have the same error when adding the url field in the list view, it’s because you’re not defining the detail view. Be sure you’re extending the RetrieveModelMixin mixin:

class UserViewSet (mixins.ListModelMixin,
                   mixins.RetrieveModelMixin,
                   viewsets.GenericViewSet):

回答 9

似乎HyperlinkedModelSerializer不同意拥有一条道路namespace。在我的应用程序中,我做了两个更改。

# rootapp/urls.py
urlpatterns = [
    # path('api/', include('izzi.api.urls', namespace='api'))
    path('api/', include('izzi.api.urls')) # removed namespace
]

在导入的urls文件中

# app/urls.py
app_name = 'api' // removed the app_name

希望这可以帮助。

It appears that HyperlinkedModelSerializer do not agree with having a path namespace. In my application I made two changes.

# rootapp/urls.py
urlpatterns = [
    # path('api/', include('izzi.api.urls', namespace='api'))
    path('api/', include('izzi.api.urls')) # removed namespace
]

In the imported urls file

# app/urls.py
app_name = 'api' // removed the app_name

Hope this helps.


回答 10

在遵循DRF快速入门指南http://www.django-rest-framework.org/tutorial/quickstart/并尝试浏览到/ users时遇到了相同的错误 。在没有问题之前,我已经完成了多次此设置。

我的解决方案不在代码中,而是在替换数据库中。

此安装与之前的安装之间的区别是我创建本地数据库时的区别。

这次我跑了

./manage.py migrate
./manage.py createsuperuser

跑步后立即

virtualenv venv
. venv/bin/activate
pip install django
pip install djangorestframework

而不是指南中列出的确切顺序。

我怀疑数据库中未正确创建某些内容。我不在乎我的开发数据库,​​所以我删除了它并运行了./manage.py migrate再次命令,创建了一个超级用户,浏览到/ users,错误消失了。

我配置DRF和db的操作顺序出现了问题。

如果您正在使用sqlite并能够测试更改为新数据库,那么在剖析所有代码之前,值得尝试一下。

I ran into the same error while I was following the DRF quickstart guide http://www.django-rest-framework.org/tutorial/quickstart/ and then attempting to browse to /users. I’ve done this setup many times before without problems.

My solution was not in the code but in replacing the database.

The difference between this install and the others before was when I created the local database.

This time I ran my

./manage.py migrate
./manage.py createsuperuser

immediately after running

virtualenv venv
. venv/bin/activate
pip install django
pip install djangorestframework

Instead of the exact order listed in the guide.

I suspected something wasn’t properly created in the DB. I didn’t care about my dev db so I deleted it and ran the ./manage.py migrate command once more, created a super user, browsed to /users and the error was gone.

Something was problematic with the order of operations in which I configured DRF and the db.

If you are using sqlite and are able to test changing to a fresh DB then it’s worth an attempt before you go dissecting all of your code.


回答 11

Bottle =序列化器.PrimaryKeyRelatedField(read_only = True)

read_only允许您表示字段,而不必将其链接到模型的另一个视图。

Bottle = serializers.PrimaryKeyRelatedField(read_only=True)

read_only allows you to represent the field without having to link it to another view of the model.


回答 12

当数据库中的段标值为空(等于”)时,我在DRF 3.7.7上收到该错误。

I got that error on DRF 3.7.7 when a slug value was empty (equals to ”) in the database.


回答 13

我遇到了同样的问题,并通过将其generics.RetrieveAPIView作为基类添加到我的视图集来解决了该问题。

I ran into this same issue and resolved it by adding generics.RetrieveAPIView as a base class to my viewset.


回答 14

我在这个错误中停留了将近2个小时:

在/ api_users / users / 1 /上配置不正确。无法使用视图名称“ users-detail”解析超链接关系的URL。您可能没有在API中包含相关模型,或者配置了错误的lookup_field在此字段上属性。

当我终于得到解决方案但不明白为什么时,所以我的代码是:

#models.py
class Users(models.Model):
    id          = models.AutoField(primary_key=True)
    name        = models.CharField(max_length=50, blank=False, null=False)
    email       = models.EmailField(null=False, blank=False) 
    class Meta:
        verbose_name = "Usuario"
        verbose_name_plural = "Usuarios"

    def __str__(self):
        return str(self.name)


#serializers.py
class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Users
        fields = (
            'id',
            'url',
            'name',        
            'email',       
            'description', 
            'active',      
            'age',         
            'some_date',   
            'timestamp',
            )
#views.py
class UserViewSet(viewsets.ModelViewSet):
    queryset = Users.objects.all()
    serializer_class = UserSerializer

#urls_api.py
router = routers.DefaultRouter()
router.register(r'users',UserViewSet, base_name='users')

urlpatterns = [ 
        url(r'^', include(router.urls)),
]

但是在我的主要网址中是:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    #api users
    url(r'^api_users/', include('usersApi.users_urls', namespace='api')),

]

所以最后我解决了擦除命名空间的问题:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    #api users
    url(r'^api_users/', include('usersApi.users_urls')),

]

我终于解决了我的问题,所以任何人都可以让我知道为什么,最好。

I was stuck in this error for almost 2 hours:

ImproperlyConfigured at /api_users/users/1/ Could not resolve URL for hyperlinked relationship using view name “users-detail”. You may have failed to include the related model in your API, or incorrectly configured the lookup_field attribute on this field.

When I finally get the solution but I don’t understand why, so my code is:

#models.py
class Users(models.Model):
    id          = models.AutoField(primary_key=True)
    name        = models.CharField(max_length=50, blank=False, null=False)
    email       = models.EmailField(null=False, blank=False) 
    class Meta:
        verbose_name = "Usuario"
        verbose_name_plural = "Usuarios"

    def __str__(self):
        return str(self.name)


#serializers.py
class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Users
        fields = (
            'id',
            'url',
            'name',        
            'email',       
            'description', 
            'active',      
            'age',         
            'some_date',   
            'timestamp',
            )
#views.py
class UserViewSet(viewsets.ModelViewSet):
    queryset = Users.objects.all()
    serializer_class = UserSerializer

#urls_api.py
router = routers.DefaultRouter()
router.register(r'users',UserViewSet, base_name='users')

urlpatterns = [ 
        url(r'^', include(router.urls)),
]

but in my main URLs, it was:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    #api users
    url(r'^api_users/', include('usersApi.users_urls', namespace='api')),

]

So to finally I resolve the problem erasing namespace:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    #api users
    url(r'^api_users/', include('usersApi.users_urls')),

]

And I finally resolve my problem, so any one can let me know why, bests.


回答 15

如果您在序列化程序中省略了字段“ id”和“ url”,则不会有任何问题。无论如何,您都可以使用json对象中返回的ID来访问帖子,这使实现前端变得更加容易。

If you omit the fields ‘id’ and ‘url’ from your serializer you won’t have any problem. You can access to the posts by using the id that is returned in the json object anyways, which it makes it even easier to implement your frontend.


回答 16

我有同样的问题,我想你应该检查一下

get_absolute_url

对象模型的方法输入值(** kwargs)标题。并在lookup_field中使用确切的字段名称

I had the same problem , I think you should check your

get_absolute_url

object model’s method input value (**kwargs) title. and use exact field name in lookup_field


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。