从fillting url django tastypie中删除空格

时间:2016-08-08 07:05:11

标签: django tastypie

我有几个用于过滤的网址。当我使用我得到的特定网址查找值时

  1. api/v1/labels/?brand_city__location__state_or_country=New%20York

  2. api/v1/labels/?brand_city__city=Novi%20Sad

  3. 使用的很多值在单词之间会有一个空格。如何从空格中删除%20并让网址更加清晰:

    所需:api/v1/labels/?brand_city__city=Novi Sad没有%20在场

    api.py

    class LocationResource(ModelResource):
        class Meta:
    
            filtering =  {
                "state_or_country": ALL
            }
    
    class CityResource(ModelResource):
    
        location = fields.ForeignKey(LocationResource, 'location', full=True)
    
        class Meta:
    
            filtering =  {
                "city": ALL,
                "location": ALL_WITH_RELATIONS
            }
    
    class LabelResource(ModelResource):
    
        brand_city = fields.ForeignKey(CityResource, 'brand_city', full=True)
    
        class Meta:
    
            filtering = {
               "brand_category": ALL,
               "brand_city": ALL_WITH_RELATIONS
            }
    

    代码段回复

    {
      "labels": [
        {
          "brand_city": {
            "city": "Manhattan",
            "location": {
              "location_choices": "State",
              "state_or_country": "New York"
            }
          }
        }
      ],
      "meta": {
        "limit": 6,
        "next": null,
        "offset": 0,
        "previous": null,
        "total_count": 1
      }
    }
    

1 个答案:

答案 0 :(得分:0)

查看python函数urllib2.unquote

# you can convert your urls using urlib2 library
path = urllib2.unquote("api/v1/labels/?brand_city__location__state_or_country=New%20York")

此外,与unquote一样,也可以使用另一个函数unquote_plus,它也可以按空格替换加号,这是取消引用HTML表单值所必需的。根据您的要求使用

相关问题