Django Rest Framework, hyperlinking a nested relationship

前端 未结 2 1361
隐瞒了意图╮
隐瞒了意图╮ 2021-02-04 17:24

I\'ve got two models: User and Ticket. Ticket has one User, User has many Tickets

2条回答
  •  -上瘾入骨i
    2021-02-04 17:51

    For the record, here is an example of the full solution based on Joe Holloway's answer:

    from rest_framework.reverse import reverse
    
    class WorkProjectSerializer(serializers.CustomSerializer):
       issues = drf_serializers.SerializerMethodField()
    
        def get_issues(self, obj):
            request = self.context.get('request')
            return request.build_absolute_uri(reverse('project-issue-list', kwargs={'project_id': obj.id}))
    
        class Meta:
            model = WorkProject
            fields = '__all__'
    

提交回复
热议问题