所以我想有我位置過濾網址爲api/v1/labels/?brand_location=Australia
過濾與澳大利亞唯一的品牌品牌迴歸,但保持收到錯誤消息:無效的資源查找資料提供(不匹配的類型)
{"error": "Invalid resource lookup data provided (mismatched type)."}
但是,當使用api/v1/labels/?brand_location/=Australia
它可以工作,但是隻會過濾澳大利亞的地點,它會返回不包含地點的完整回覆。
所以我的問題是:
- 我怎樣才能去掉結尾的斜線?並得到它過濾澳大利亞只有地點
- 這是正確的方式來解決這個問題嗎?當用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
}
}
這是新的我在使用上面提到的URL時遇到了錯誤:{「error」:「'brand_location'字段中的查詢不允許超過一個級別。}} – Amechi