1.1 course/urls.py 中还是查询课程的路由
router.register(r'course', views.CourseViewSet)
1.2 course/serializers.py 中添加评论相关查询
from goods.serializers import GoodsSerializer class CourseDeepSerializer(CourseSerializer): comment = CommentSerializer(many=True) class CommentSerializer(serializers.ModelSerializer): user = serializers.CharField(source='user.username') to_user = serializers.SerializerMethodField(required=False) class Meta: model = Comment fields = '__all__' def get_to_user(self,row): if row.to_user: return row.to_user.username return ''
2.测试获取评论接口
http://192.168.56.100:8888/course/course/1/
3.前端代码
srccomponentscourse_show.vue
srccomponentscommoncomment.vue