我有两个ModelResource
sAttack
和Packet
,由 ManyToManyField 关联。
这往往表现为Packet.attacks
包含 1-3 次攻击,并Attack.packets
包含数百个数据包。
我一直计划这样做的方式是:每个AttackResource
都有一个Attack.packets
属性,该属性是指向包含查询集的链接,Attack.packets
并且查询集嵌套在AttackResource
.
IE:
/api/attack/1/ # contains attribute with URL of /api/attack/1/packets/
/api/attack/1/packets/ # contains all packets where attack with id=1 is in Packet.attacks
我怎样才能做到这一点?
我试过按照cyberdelia的要点来嵌套资源,但/api/attack/1/packets/
实际上并不包含attack.id在packet.attacks中的数据包。
我的ModelResource
课程与要点相同,然后我的其他资源是:
class AttackResource(ModelResource):
packets = fields.ToManyField('honeywall.api.PacketResource', 'packets', 'attack')
class Meta:
queryset = Attack.objects.all()
resource_name = 'attack'
和
class PacketResource(ModelResource):
attacks = fields.ToManyField('honeywall.api.AttackResource', 'attacks', 'packet')
class Meta:
queryset = Packet.objects.all()
resource_name = 'packet'
filtering = {
'attacks': ALL_WITH_RELATIONS,
}