簡體   English   中英

用相關對象擴展stylishpie API

[英]Extending tastypie API with related objects

我是Django,Tastypie的新手,並在這里提問。

我有一個使用Deliciouspie的帶有API的Django應用程序。 如果我對/api/v1/ou/33/發出GET請求,我的API會返回id == 33的對象,可以。

{
  "child_ou_uri": "/api/v1/ou/33/child_ou/",
  "displayname": "Mother",
  "id": 33,
  "inherit": true,
  "name": "Mother",
  "resource_uri": "/api/v1/ou/33/"
}

問題是,我正在嘗試擴展API,以便它通過上述對象中的child_ou_uri URI返回相關對象。 孩子是與父母同類型的對象。 該模型具有一個指向其父級pk的屬性parent_id

我的OuResource看起來像這樣:

class OuResource(ModelResource):

    class Meta:
        queryset = OU.objects.all()
        resource_name = 'ou'
        list_allowed_methods = ['get']
        detail_allowed_methods = ['get']
        filtering = {
            'name': ['icontains'],
        }

        authentication = SessionAuthentication()
        authorization = OperatorLocationAuthorization()

    def get_child_ou(self, request, **kwargs):
        self.method_check(request, ['get', ])

        ous = OuResource().get_list(request, parent_id=kwargs['pk'])

        return ous

    def prepend_urls(self):

        return [
            url(r'^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/child_ou%s$' % (self._meta.resource_name, '/'),
            self.wrap_view('get_child_ou'),
            name='api_get_child_ou')
        ]

    def dehydrate(self, bundle):
        kwargs = dict(api_name='v1', resource_name=self._meta.resource_name, pk=bundle.data['id'])

        bundle.data['child_ou_uri'] = reverse('api_get_child_ou', kwargs=kwargs)

        return bundle

當我導航到/api/v1/ou/33/child_ou/我想獲取其屬性parent_id設置為33的子對象列表,但是我得到的所有對象都沒有任何過濾,等效對我來說導航到/api/v1/ou/

{
  "meta": {
    "limit": 20,
    "next": "/api/v1/ou/?offset=20&limit=20&format=json",
    "offset": 0,
    "previous": null,
    "total_count": 29
  },
  "objects": [
    {
      "child_ou_uri": "/api/v1/ou/33/child_ou/",
      "displayname": "Mother",
      "id": 33,
      "inherit": true,
      "name": "Mother",
      "resource_uri": "/api/v1/ou/33/"
    },
    {
      "child_ou_uri": "/api/v1/ou/57/child_ou/",
      "displayname": "Mothers 1st child",
      "id": 57,
      "inherit": true,
      "name": "Child 1",
      "resource_uri": "/api/v1/ou/57/"
    },
    {
      "child_ou_uri": "/api/v1/ou/58/child_ou/",
      "displayname": "Mothers 2nd child",
      "id": 58,
      "inherit": true,
      "name": "Child 2",
      "resource_uri": "/api/v1/ou/58/"
    }
  ]
}

我在這里想念什么?

[解]

Gareth的回答使我走上了正確的道路。 我將OuResource更改為如下所示。 這使我可以導航到/api/v1/ou/33/child_ous/這樣的網址,該網址返回子對象的自定義json。

class OuResource(ModelResource):
    class Meta:
        queryset = OU.objects.all()
        resource_name = 'ou'
        list_allowed_methods = ['get']
        detail_allowed_methods = ['get']
        filtering = {
            'name': ['icontains'],
        }

        authentication = SessionAuthentication()
        authorization = OperatorLocationAuthorization()

    def prepend_urls(self):
        return [
            url(r"^(?P<resource_name>%s)/(?P<ou_id>\d+)/child_ous%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('child_ous'), name="api_child_ous"),
        ]

    def child_ous(self, request, **kwargs):
        self.method_check(request, allowed=['get'])
        self.is_authenticated(request)
        self.throttle_check(request)

        ous = list(OU.objects.filter(parent_id=kwargs['ou_id']))

        data = []
        for x in ous:
            data.append({
                'id' : x.id,
                'name' : x.name,
                'parent_id' : x.parent_id
            })

        return JsonResponse(data, safe=False)

首先,查看有關創建搜索的stylishpie文檔。

不嵌套就更容易做到這一點,例如/ api / v1 / ou_related /?to = 58,但是嵌套可能是可取的,因為它具有表現力。

對於具有分頁的嵌套搜索,請查看創建另一個資源OuSearchResource。 該資源將覆蓋authorized_read_list (可能還有get_list)以傳遞必要的詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM