慕运维2186032
2021-03-08 19:46
AssertionError: `HyperlinkedIdentityField` requires the request in the serializer context. Add `context={'request': request}` when instantiating the serializer.
老师这个报错是什么情况
class CourseSerializer(serializers.ModelSerializer): teacher = serializers.ReadOnlyField(source='teacher.username') # 外键字段只读 class Meta: model = Course # exclude = ("id",) # 注意元组中只有1个元素时不能写成("id") # fields = ('name', 'introduction', 'teacher', 'price') fields = '__all__' depth = 2 # class CourseSerializer(serializers.HyperlinkedModelSerializer): # teacher = serializers.ReadOnlyField(source='teacher.username') # 外键字段只读 # class Meta: # model = Course # # url是默认值, 可在settings.py中设置URL_FIELD_NAME使全局生效 # fields = ('id','url', 'name', 'introduction', 'teacher', 'price')
用上面的
Django REST framework前后端分离框架实践
6880 学习 · 34 问题
相似问题