具有多个查找参数的Djangorestframework超链接标识符字段

2024-05-17 01:35:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我的urlpatterns中有以下URL:

url(r'^user/(?P<user_pk>[0-9]+)/device/(?P<uid>[0-9a-fA-F\-]+)$', views.UserDeviceDetailView.as_view(), name='user-device-detail'),

注意它有两个字段:user_pk,和uid。URL看起来像:https://example.com/user/410/device/c7bda191-f485-4531-a2a7-37e18c2a252c

在这个模型的详细视图中,我试图填充一个url字段,该字段将包含返回模型的链接。

在序列化程序中,我有:

url = serializers.HyperlinkedIdentityField(view_name="user-device-detail", lookup_field='uid', read_only=True)

但是,我认为它失败了,因为URL有两个字段:

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

当URL有两个或多个URL模板项时,如何使用HyperlinkedIdentityField(或任何Hyperlink*Field)?(查找字段)?


Tags: thename模型viewurlfielduiddevice