Python Django REST框架超链接URL解析失败
我试图在我的Django REST框架API中设置超链接,而在我的一生中,我无法找出我的错误在哪里 我的模型:Python Django REST框架超链接URL解析失败,python,django,rest,serialization,django-rest-framework,Python,Django,Rest,Serialization,Django Rest Framework,我试图在我的Django REST框架API中设置超链接,而在我的一生中,我无法找出我的错误在哪里 我的模型: class Franchise(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=255) # Other fields 我的序列化程序 class FranchiseListSerializer(serializers.Hyper
class Franchise(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
# Other fields
我的序列化程序
class FranchiseListSerializer(serializers.HyperlinkedModelSerializer):
url = serializers.HyperlinkedIdentityField(
view_name='franchise_details',
lookup_field='id',
lookup_url_kwarg='franchiseid'
)
class Meta:
model = Franchise
fields = ('id', 'name', 'url')
我的网址:
url(r'^db/franchise/$', views.franchise_index, name='db_franchise_index'),
url(r'^db/franchise/(?P<franchiseid>[0-9]+)/$', views.franchise_details, name='db_franchise_details')
请注意,上面看到的FranshiseDetailSerializer
工作正常
总结:
- URL/api/db/Francision/转到视图
,该视图返回由Francision\u index
特许列表序列化程序序列化的数据
- URL/api/db/特许经营/
/进入查看特许经营详细信息,该视图返回由特许经营详细信息序列化程序序列化的数据(工作正常)
特许经营列表序列化程序
,我希望该字段链接到相应的特许经营详细信息页面。在我添加url字段之前,序列化程序只返回id
和name
,这是当时正确的预期行为
当我转到/api/db/francement/now时,我得到一个错误:
ImproperlyConfigured at /api/db/franchise/
Could not resolve URL for hyperlinked relationship using view name "franchise_details". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.
根据这个提示,我在特许列表序列化程序
的url
字段中设置了参数,据我所知,它们是正确的。我已经检查并再次检查了DRF文档,但是没有找到解决方案
继Stackoverflow上的其他类似问题之后,我尝试将查看\u name='特许经营\u详细信息'
更改为查看\u name='api:特许经营\u详细信息'
(相关文件所在的Django应用程序的名称)和查看\u name='api:特许经营\u详细信息-detail'
,但没有效果
感谢所有的帮助,干杯。多亏@AKS的提示,我明白了
view\u name
实际上是URL的名称,而不是视图的名称。从我阅读文档的方式来看(事实上它是view\u name
而不是url\u name
),它似乎说它应该是视图的名称
实际上,我以前曾尝试过使用
查看\u name='db\u特许经营权\u详细信息'
(我的URL名称),但没有成功。在AKS向我推荐了这条评论后,我再次尝试,并尝试使用view\u name='api:db\u特许经营权\u details'
,这确实有效 多亏@AKS的提示,我才明白了这一点view\u name
实际上是URL的名称,而不是视图的名称。从我阅读文档的方式来看(事实上它是view\u name
而不是url\u name
),它似乎说它应该是视图的名称
实际上,我以前曾尝试过使用
查看\u name='db\u特许经营权\u详细信息'
(我的URL名称),但没有成功。在AKS向我推荐了这条评论后,我再次尝试,并尝试使用view\u name='api:db\u特许经营权\u details'
,这确实有效 在URL中,您可以查看\u name'db\u特许经营权\u详细信息'
,但在序列化程序中,您可以查看特许经营权\u详细信息'
,因此不匹配。啊,文档似乎不这么说,至少我是这么看的。Cheers在URL中,您可以查看\u name'db\u特许权\u详细信息'
,但在序列化程序中,您有'franciple\u详细信息'
,因此不匹配。啊,文档似乎不是这样说的,至少我是这么看的。干杯
ImproperlyConfigured at /api/db/franchise/
Could not resolve URL for hyperlinked relationship using view name "franchise_details". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.