7

如果运行以下代码,我会超出最大递归深度:

from tastypie import fields, utils
from tastypie.resources import ModelResource
from core.models import Project, Client


class ClientResource(ModelResource):
    projects = fields.ToManyField(
        'api.resources.ProjectResource', 'project_set', full=True
    )
    class Meta:
        queryset = Client.objects.all()
        resource_name = 'client'


class ProjectResource(ModelResource):
    client = fields.ForeignKey(ClientResource, 'client', full=True)
    class Meta:
        queryset = Project.objects.all()
        resource_name = 'project'

# curl http://localhost:8000/api/client/?format=json
# or
# curl http://localhost:8000/api/project/?format=json

如果 set full=False 在其中一个关系上有效。我确实理解为什么会发生这种情况,但我需要两种关系来带来数据,而不仅仅是“resource_uri”。有没有一种美味的方法来做到这一点?我设法解决了在我的项目模型上创建序列化方法的问题,但它远非优雅。谢谢。

4

3 回答 3

13

您必须full_dehydrate在至少一个资源上覆盖方法以跳过导致递归的脱水相关资源。

或者,您可以定义两种类型的资源,它们使用相同的模型,一种使用full=True,另一种使用full=False

于 2012-07-19T23:16:07.380 回答
3

感谢@astevanovic 指出正确的方向。

我发现dehydrate仅处理某些指定字段的覆盖full_hydrate方法比跳过字段的覆盖方法要少一些乏味。

为了追求可重用性,我想出了以下代码片段。希望它对某些人有用:

class BeeModelResource(ModelResource):

    def dehydrate(self, bundle):
        bundle = super(BeeModelResource, self).dehydrate(bundle)
        bundle = self.dehydrate_partial(bundle)        
        return bundle

    def dehydrate_partial(self, bundle):
        for field_name, resource_field in self.fields.items():
            if not isinstance(resource_field, RelatedField):
                continue

            if resource_field.full: # already dehydrated
                continue

            if not field_name in self._meta.partial_fields:
                continue

            if isinstance(resource_field, ToOneField):
                fk_object = getattr(bundle.obj, resource_field.attribute)
                fk_bundle = Bundle(obj=fk_object, request=bundle.request)
                fk_resource = resource_field.get_related_resource(fk_object)

                bundle.data[field_name] = fk_resource.dehydrate_selected( 
                        fk_bundle, self._meta.partial_fields[field_name]).data
            elif isinstance(resource_field, ToManyField):
                data = []

                fk_objects = getattr(bundle.obj, resource_field.attribute)
                for fk_object in fk_objects.all():
                    fk_bundle = Bundle(obj=fk_object, request=bundle.request)
                    fk_resource = resource_field.get_related_resource(fk_object)
                    fk_bundle = fk_resource.dehydrate_selected_fields( 
                            fk_bundle, self._meta.partial_fields[field_name])
                    data.append(fk_bundle.data)
                bundle.data[field_name] = data

        return bundle

    def dehydrate_selected_fields(self, bundle, selected_field_names):
        # Dehydrate each field.
        for field_name, field_object in self.fields.items():
            # A touch leaky but it makes URI resolution work. 
            # (borrowed from tastypie.resources.full_dehydrate)
            if field_name in selected_field_names and not self.is_special_fields(field_name):
                if getattr(field_object, 'dehydrated_type', None) == 'related':
                    field_object.api_name = self._meta.api_name
                    field_object.resource_name = self._meta.resource_name

                bundle.data[field_name] = field_object.dehydrate(bundle)

        bundle.data['resource_uri'] = self.get_resource_uri(bundle.obj)
        bundle.data['id'] = bundle.obj.pk

       return bundle

    @staticmethod
    def is_special_fields(field_name):
        return field_name in ['resource_uri']

对于 @sigmus 的示例,资源将需要 3 处修改:

  1. 两种资源都将BeeModuleResource用作其超类(或者,添加dehydrate_partial到一个资源和另一个资源dehydrate_selected。)
  2. full=True在任一资源上取消设置
  3. 将未设置的资源添加partial_fields到资源中Meta

```

class ClientResource(BeeModelResource): # make BeeModelResource a super class
    projects = fields.ToManyField(
        'api.resources.ProjectResource', 'project_set'
    ) # remove full=True
    class Meta:
        queryset = Client.objects.all()
        resource_name = 'client'
        partial_fields = {'projects': ['memo', 'title']} # add partial_fields

class ProjectResource(BeeModelResource): # make BeeModelResource a super class
    client = fields.ForeignKey(ClientResource, 'client', full=True)
    class Meta:
        queryset = Project.objects.all()
        resource_name = 'project'
于 2013-07-18T07:09:22.253 回答
3

死的简单解决方案:在两个关系字段上设置use_in = 'list'kwarg!

文档:http ://django-tastypie.readthedocs.org/en/latest/fields.html#use-in

于 2014-06-05T12:46:31.977 回答