2013-07-25 30 views
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”,它映射到“IMEI”在设备与ForeignKey的Tastypie |当Django的ForeignKey的使用“to_field”和相关资源不是主键

我想会有一些属性to_field与我可以写

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

但有没有这样的事情在tastypie

这里是我的设备和更新型号

http://pastebin.com/ENA64RtM

+0

你检查RELATED_FIELD参数(在ressources)和[ related_name](http://django-tastypie.readthedocs.org/en/latest/fields.html#related-name)(在模型中)? – nnaelle

+0

@nnaelle对不起,我没有完全让你。我做了这个'device = fields.ForeignKey(DeviceResource,attribute ='device',related_name ='imei',full = True,null = True)''和'device = fields.ForeignKey(DeviceResource,attribute ='device',related_field ='imei',full = True,null = True)',这不起作用 – ZenOut

回答

1

我不认为tastypie支持这一点,所以如果你可以改变你的模型使用隐式主键我会这样做。

也就是说,属性arg是指您需要访问相关实例的Django模型属性,因此如果您尚未尝试使用attribute='imei'

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

如果你只需要过滤的入眼,试试这个:

filtering = { 
    device: "ALL_WITH_RELATIONS" 
} 

然后你UpdateResource过滤器的呼叫会像

/api/v1/update/?device__imei=asdf123... 
+0

attribute ='imei'神奇地工作,非常感谢。 – ZenOut

相关问题