sig*_*mus 7 python django tastypie
如果运行以下代码,我会超出最大递归深度:
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
Run Code Online (Sandbox Code Playgroud)
如果其中一个关系上的set full = False,则它起作用.我确实理解为什么会发生这种情况,但我需要两种关系才能带来数据,而不仅仅是"resource_uri".是否有Tastypie方法来做到这一点?我设法解决了在我的项目模型上创建序列化方法的问题,但它远非优雅.谢谢.
ast*_*vic 13
您必须覆盖full_dehydrate至少一个资源上的方法,以跳过导致递归的脱水相关资源.
或者,您可以定义两种类型的资源,它们使用相同的模型full=True,另一种使用相同的模型full=False.
| 归档时间: |
|
| 查看次数: |
1725 次 |
| 最近记录: |