1
class DeviceResource(ModelResource):
    class Meta :
        queryset = Device.objects.all()
        resource_name='device'


class UpdateResource(ModelResource):
    device = fields.ForeignKey(DeviceResource, attribute='device',full=True, null=True)

    class Meta : 
        queryset = Update.objects.all()
        resource_name = 'update'
        filtering = {'imei' : ALL } 

更新模型有一个字段“imei”,它使用 ForeignKey 映射到设备中的“imei”

我认为会有一些属性 to_field 可以用来写

        device = fields.ForeignKey(DeviceResource, to_field='imei'attribute='device',full=True, null=True)

但是美味的派里没有这样的东西

这是我的设备和更新模型

http://pastebin.com/ENA64RtM

4

1 回答 1

1

我认为tastepie 不能很好地支持这一点,所以如果您可以更改模型以使用隐式主键,我会这样做。

也就是说,属性 arg 是指您需要访问相关实例的 Django 模型属性,所以attribute='imei'如果您还没有,请尝试一下。

如果您需要通过 IMEI 引用 DeviceResources 并且不知道他们的 .pk,请参阅tastepie 文档以获取有关非 pk 查找的更多帮助。

如果您只需要过滤 GET,请尝试以下操作:

filtering = {
    device: "ALL_WITH_RELATIONS"
}

然后您的 UpdateResource 过滤器调用将类似于

/api/v1/update/?device__imei=asdf123...
于 2013-07-25T20:07:25.097 回答