2016-08-07 40 views
0

所以我想有我位置過濾網址爲api/v1/labels/?brand_location=Australia過濾與澳大利亞唯一的品牌品牌迴歸,但保持收到錯誤消息:無效的資源查找資料提供(不匹配的類型)

{"error": "Invalid resource lookup data provided (mismatched type)."} 

但是,當使用api/v1/labels/?brand_location/=Australia它可以工作,但是隻會過濾澳大利亞的地點,它會返回不包含地點的完整回覆。

所以我的問題是:

  1. 我怎樣才能去掉結尾的斜線?並得到它過濾澳大利亞只有地點
  2. 這是正確的方式來解決這個問題嗎?當用django tastypie使用外鍵時?

我下面的代碼:

Models.py

class Brand(models.Model): 

    brand_location = models.ForeignKey('Location', null=True, blank=True, default="") 


class Location(models.Model): 

    state_or_country = models.CharField(unique=True, max_length=200, blank=True, default="", verbose_name=_('Location'), 

api.py

class LocationResource(ModelResource): 
    class Meta: 
     excludes = ['modified', 'id', 'created'] 

     queryset = Location.objects.all() 

     resource_name = 'locations' 

class LabelResource(ModelResource): 

    brand_location = fields.ForeignKey(LocationResource, 'brand_location', full=True) 

    class Meta: 

     filtering = { 
      "brand_location": ALL 
     } 

     queryset = Brand.objects.all() 

     resource_name = 'labels' 

片段JSON響應

{ 
    "labels": [ 
    { 
     "brand_location": { 
     "state_or_country": "Australia" 
     } 
    } 
    ], 
    "meta": { 
    "limit": 6, 
    "next": "/unlabel-network/unlabel-network-api/v1/labels/?limit=6&brand_location%2F=Australia&offset=6", 
    "offset": 0, 
    "previous": null, 
    "total_count": 128 
    } 
} 

回答

0

api/v1/labels/?brand_location=Australia尋找Location.id=Australia

允許過濾更深:

filtering = { 
    "brand_location": ALL_WITH_RELATIONS 
} 

,尋找state_or_country領域:

api/v1/labels/?brand_location__state_or_country=Australia 
+0

這是新的我在使用上面提到的URL時遇到了錯誤:{「error」:「'brand_location'字段中的查詢不允許超過一個級別。}} – Amechi

0

我只是添加filteringLocationResource然後加入ALL_WITH_RELATIONS到我的過濾字典對我LabelResource

需要
class LocationResource(ModelResource): 
    class Meta: 
     excludes = ['modified', 'id', 'created'] 

     queryset = Location.objects.all() 

     resource_name = 'locations' 

     filtering = { 
      "state_or_country": ALL 
     } 

class LabelResource(ModelResource): 

    brand_location = fields.ForeignKey(LocationResource, 'brand_location', full=True) 

    class Meta: 

     filtering = { 
      "brand_location": ALL, 
      "brand_location": ALL_WITH_RELATIONS 
     } 

     queryset = Brand.objects.all() 

     resource_name = 'labels' 
相關問題