2013-07-25 65 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

相關問題