本文介绍了当反向关系为full = True时,Django Tastypie会抛出“最大递归深度超出”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果运行下面的代码,我得到了一个最大递归深度:

I get a maximum recursion depth exceeded if a run the code below:

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。有没有Tastypie的方法呢?我设法解决了在我的项目模型中创建一个序列化方法的问题,但它远非优雅。谢谢。

If a set full=False on one of the relations it works. I do understand why this is happening but I need both relations to bring data, not just the "resource_uri". Is there a Tastypie way to do it? I managed to solve the problem creating a serialization method on my Project Model, but it is far from elegant. Thanks.

推荐答案

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

You would have to override full_dehydrate method on at least one resource to skip dehydrating related resource that is causing the recursion.

或者,您可以定义两种类型的资源,使用相同的模型,资源为$ code> full = True 另一个 full = False

Alternatively you can define two types of resources that use the same model one with full=Trueand another with full=False.

这篇关于当反向关系为full = True时,Django Tastypie会抛出“最大递归深度超出”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 15:59