Including child resources in a Django Tastypie API

≯℡__Kan透↙ 提交于 2019-11-30 09:23:15

Can you post your solution?

I have the same use case/models (a foreign key relation ship established in the "child table" pointing back to the parent table) but cannot resolve it.

The solution I have seen is to add this line to your TicketResource a the class level NOT within the meta subclass:

comments = fields.ToManyField('TicketCommentResource', 'ticket', full=True)

Then possibly also add this to TicketCommentResource, again at the class level:

event = fields.ForeignKey('TicketResource', 'event')

But I always get the error that my parent object resource (in your example TicketResource) does not have the attribute 'ticket' which is the second term sent to ToManyField.

I have tried to play with this a lot but can't seem to get the winning combo. The solution you originally posted works but as you yourself pointed out it not ideal.

Thanks!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!