DRF 无法使用 PrimaryKeyRelatedField 上的视图名称解析超链接关系的 URL
Posted
技术标签:
【中文标题】DRF 无法使用 PrimaryKeyRelatedField 上的视图名称解析超链接关系的 URL【英文标题】:DRF Could not resolve URL for hyperlinked relationship using view name on PrimaryKeyRelatedField 【发布时间】:2021-04-03 01:00:51 【问题描述】:我对 DRF 序列化程序上的 POST 请求有一个令人沮丧的问题 - 由于某种原因,DRF 转到了不正确的视图名称,并且view_name
不是PrimaryKeyRelated Field
上的可设置属性。
型号:
# (the class with the issue)
class Section(models.Model):
teacher = models.ManyToManyField(Teacher)
# (a class that works, using the same pattern)
class Assessment(models.Model):
standards = models.ManyToManyField(Standard)
序列化器:
# (doesn't work)
class SectionInfoSerializer(serializers.HyperlinkedModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name="gbook:section-detail")
teacher = serializers.PrimaryKeyRelatedField(many=True, read_only=True),
teachers_id = serializers.PrimaryKeyRelatedField(write_only=True, queryset=Teacher.objects.all(), many=True, source='teacher', allow_empty=False)
class Meta:
model = Section
fields = '__all__'
read_only_fields = ['sendEmails', 'teacher', 'course']
# (works)
class AssessmentSerializer(serializers.HyperlinkedModelSerializer):
pk = serializers.PrimaryKeyRelatedField(read_only=True)
url = serializers.HyperlinkedIdentityField(view_name="appname:assessments-detail")
standards = serializers.PrimaryKeyRelatedField(read_only=True, many=True)
standards_id = serializers.PrimaryKeyRelatedField(queryset=Standard.objects.all(), source='standards', write_only=True, many=True, allow_empty=False)
class Meta:
model = Assessment
fields = '__all__'
网址:
router.register(r'teachers', teacher_views.TeacherViewSet, basename='teacher')
router.register(r'sections', course_views.SectionViewSet)
router.register(r'standards', gbook.views.standard_views.StandardViewSet, basename='standards')
router.register(r'assessments', AssessmentViewSet, basename='assessments')
我在 POST 和 PUT 期间使用_id
字段来发送相关对象的 id,然后对其进行序列化。这对AssessmentSerializer
(和其他几个)非常有效,但由于我无法弄清楚的原因而失败。当然,错误中返回的视图中缺少 appname,但我不知道为什么会发生这种情况,以及为什么以前没有发生过。
堆栈跟踪:
Internal Server Error: /appname/sections/
Traceback (most recent call last):
File "/venv2/lib/python3.8/site-packages/rest_framework/relations.py", line 393, in to_representation
url = self.get_url(value, self.view_name, request, format)
File "/venv2/lib/python3.8/site-packages/rest_framework/relations.py", line 331, in get_url
return self.reverse(view_name, kwargs=kwargs, request=request, format=format)
File "/venv2/lib/python3.8/site-packages/rest_framework/reverse.py", line 47, in reverse
url = _reverse(viewname, args, kwargs, request, format, **extra)
File "/venv2/lib/python3.8/site-packages/rest_framework/reverse.py", line 60, in _reverse
url = django_reverse(viewname, args=args, kwargs=kwargs, **extra)
File "/venv2/lib/python3.8/site-packages/django/urls/base.py", line 87, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/venv2/lib/python3.8/site-packages/django/urls/resolvers.py", line 685, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'teacher-detail' not found. 'teacher-detail' is not a valid view function or pattern name.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/lib/python3.8/site-packages/django/core/handlers/base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/lib/python3.8/site-packages/rest_framework/viewsets.py", line 114, in view
return self.dispatch(request, *args, **kwargs)
File "/lib/python3.8/site-packages/rest_framework/views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "/lib/python3.8/site-packages/rest_framework/views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "/lib/python3.8/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
raise exc
File "/lib/python3.8/site-packages/rest_framework/views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "/lib/python3.8/site-packages/rest_framework/mixins.py", line 20, in create
headers = self.get_success_headers(serializer.data)
File "/lib/python3.8/site-packages/rest_framework/serializers.py", line 562, in data
ret = super().data
File "/lib/python3.8/site-packages/rest_framework/serializers.py", line 260, in data
self._data = self.to_representation(self.instance)
File "/lib/python3.8/site-packages/rest_framework/serializers.py", line 529, in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "/lib/python3.8/site-packages/rest_framework/relations.py", line 533, in to_representation
return [
File "/lib/python3.8/site-packages/rest_framework/relations.py", line 534, in <listcomp>
self.child_relation.to_representation(value)
File "/lib/python3.8/site-packages/rest_framework/relations.py", line 408, in to_representation
raise ImproperlyConfigured(msg % self.view_name)
django.core.exceptions.ImproperlyConfigured: Could not resolve URL for hyperlinked relationship using view name "teacher-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.
[24/Dec/2020 09:55:04] "POST /appname/sections/ HTTP/1.1" 500 157269
【问题讨论】:
我几乎可以肯定您没有名为teacher-detail
的视图/网址,但可能是 @987654332 @。您可以使用this method 列出项目中的所有 URL 模式,并使用 grep
表达式查看实际的 URL 名称是什么
正确 - teacher-detail
不是视图:应该是 appname:teacher-detail
。但是,没有办法在我知道的那个字段上设置正确的视图名称
有一个teachers_id
(复数),但有一个teacher
字段(单数)。复制/粘贴错误还是真正的问题?
您可以使用HyperlinkedRelatedField
和view_name
参数@DeltaG
@JPG PrimaryKeyRelatedField 没有理由需要视图。那就是问题所在。它应该只是将 id 粘贴在数据库字段中。
【参考方案1】:
所以,我错过的是这些是 HyperlinkedModelSerializer。评估的不同之处在于您如何处理多对多。
HyperlinkedModelSerializer,为相关字段生成 HyperlinkedRelatedField 并从 rest_framework.utils.get_detail_view_name
生成 view_name,它没有应用程序名称的功能。
这是由build_field
完成的,它根据从rest_framework.utils.model_meta.get_field_info()
获得的模型信息委托给build_relational_field
。
您的teacher
字段可能在其中。
我不确定为什么它适用于评估,因为我也找不到拒绝/接受的条件,但我的直觉说,因为 PrimaryKeyRelatedField 是一个相关字段,它不会构建 HyperlinkedRelatedField。
无论哪种方式,您都应该查看传递给 build_relational_field
的字段名称以弄清楚这一点。
解决方案
去掉teacher字段定义后面的逗号:
teacher = serializers.PrimaryKeyRelatedField(many=True, read_only=True),
------^
这会将教师变成一个元组,并且教师 ID 也会消失。结果,创建了标准的超链接相关字段:
SectionInfoSerializer(instance=<Section: Section object (1)>):
url = HyperlinkedIdentityField(view_name='gbook:section-detail')
teachers_id = PrimaryKeyRelatedField(allow_empty=False, many=True, queryset=<QuerySet [<Teacher: Teacher object (1)>]>, source='teacher', write_only=True)
send_emails = BooleanField(required=False)
teacher = HyperlinkedRelatedField(allow_empty=False, many=True, read_only=True, view_name='teacher-detail')
course = HyperlinkedRelatedField(allow_empty=False, many=True, read_only=True, view_name='course-detail')
【讨论】:
Course
没有老师。没有一个相关模型有任何包含Teacher
的相关对象,并且teacher-detail
(前面没有appname)在项目的任何地方都不存在
已更新。希望这能让你到达那里,但如果不逐步了解,很难看出真正的罪魁祸首在哪里。
感谢您的线索 - 我正在追踪它。值得注意的是,如果teacher
是PrimaryKeyRelatedField
,这仍然不起作用。
看起来build_relational_field
没有为此字段调用。我在调试器处理 course_id
时受到了打击(另一个以完全相同的方式构建的字段),但在正确工作的评估序列化程序中,standards_id
却没有
这是一个绝妙的收获!我还能够通过修改模型中的字段来解决它,但逗号仍然存在:teachers = models.ManyToManyField(Teacher, db_table='gbook_section_teacher')
...但逗号确实是解决它的一种简单方法。谢谢!以上是关于DRF 无法使用 PrimaryKeyRelatedField 上的视图名称解析超链接关系的 URL的主要内容,如果未能解决你的问题,请参考以下文章
无法使用用户名/密码登录令牌端点:drf 令牌身份验证和自定义用户模型,Django
DRF auth_token:“non_field_errors”:[“无法使用提供的凭据登录。”
解决drf_yasg中的SwaggerAPI无法正确分组问题