2012-09-14 35 views
4

我有兩個模型是這樣的:如何在外地製作tastypie過濾器?

class CompanyResource(ModelResource): 
    class Meta: 
     queryset = Company.objects.all() 
     fields = ['title', 'latitude', 'longitude'] 
     resource_name = 'company' 
     filtering = { 
      'latitude': ALL, 
      'longitude': ALL 
     } 

class EventResource(ModelResource): 
    company = fields.ToOneField(CompanyResource, 'company', full=True) 
    class Meta: 
     fields = ['title', 'company'] 
     queryset = Event.objects.all() 
     resource_name = 'event' 
     filtering = { 
      'company': ALL_WITH_RELATIONS 
     } 

然後我嘗試訪問/api/v1/event/?format=json&company_latitude__within=2,3/api/v1/event/?format=json&company_latitude__lt=1則不會被過濾緯度:

{ 
    "meta": { 
     "limit": 20, 
     "next": "/api/v1/event/?offset=20&limit=20&format=json", 
     "offset": 0, 
     "previous": null, 
     "total_count": 329 
    }, 
    "objects": [ 
     { 
      "company": { 
       "latitude": "1.30521100000000", 
       "longitude": "103.81116299999996", 
       "resource_uri": "" 
      }, 
      "resource_uri": "/api/v1/event/16/", 
      "title": "50% off at [email protected], $50 for $100 worth of Fine Dining" 
     } 
    ] 
} 

我怎樣才能使這項工作?

回答

4

哦,這是因爲兩件事。我不能在Django中做field__within(我爲什麼這麼想?),它應該是/api/v1/event/?format=json&company__latitude__lt=2

+1

所以它是雙下劃線來通過外鍵進行連接。禮包! – Valer