如何使用一个URI获取多个资源?

时间:2019-11-05 11:07:39

标签: tastypie

当我发出以下curl命令时: curl http://localhost/refeq/v1/vehiclesequipements/?format=json -s | jq . 我得到:

{
    "objects":[
    { 
      "vehicle_id": 9759,
      "resource_uri": "/refeq/v1/vehiclesequipements/9759/",
      "noArticle": "",
      "name": "OBCIV_TFT2",
      "mac": "84:7e:40:e9:f2:1e"
    },
    {
      "vehicle_id": 9899,
      "resource_uri": "/refeq/v1/vehiclesequipements/9899/",
      "noArticle": "",
      "name": "FILM_FRONT",
      "mac": "00:40:9d:a2:36:fe"
    },
    {
      "vehicle_id": 9899,
      "resource_uri": "/refeq/v1/vehiclesequipements/9899/",
      "noArticle": "",
      "name": "OBCIV_TFT1",
      "mac": "84:7e:40:ea:a4:36"
    }
  ],
  "meta": {
    "total_count": 3,
    "offset": 0,
    "limit": 0
  }
}

我想获得关于vehicle_id的两个资源:9899

得到这样的东西:

{
    "objects":[
    {
      "vehicle_id": 9899,
      "resource_uri": "/refeq/v1/vehiclesequipements/9899/",
      "noArticle": "",
      "name": "FILM_FRONT",
      "mac": "00:40:9d:a2:36:fe"
    },
    {
      "vehicle_id": 9899,
      "resource_uri": "/refeq/v1/vehiclesequipements/9899/",
      "noArticle": "",
      "name": "OBCIV_TFT1",
      "mac": "84:7e:40:ea:a4:36"
    }
  ],
  "meta": {
    "total_count": 2,
    "offset": 0,
    "limit": 0
  }
}

我面临的问题是 curl http://localhost/refeq/v1/vehiclesequipements/9899/?format=json -s 返回: More than one resource is found at this URI.

如何返回具有ressource_uri的多个资源?

我看了看文档,但我不知道该如何实现..

这是我的api.py

from tastypie.resources import ModelResource
from tastypie.serializers import Serializer
from tastypie.authorization import Authorization
from refeq.models import VehiclesEquipements

USE_LOCAL_TIME = True

class MyDateSerializer(Serializer):
    def format_datetime(self, data):
        return data.strftime("%Y-%m-%dT%H:%M:%S")

class VehiclesEquipementsResource(ModelResource):
    class Meta:
        queryset = VehiclesEquipements.objects.all()
        resource_name = 'vehiclesequipements'
        #filtering = {"vehicle_id":["exact","in"]}
        authorization=Authorization()
        limit = 0
        max_limit = 0
        if USE_LOCAL_TIME:
            serializer = MyDateSerializer()

和models.py

class VehiclesEquipements(models.Model):
    vehicle_id = models.IntegerField(primary_key=True)
    noArticle = models.CharField(max_length=30, blank=True)
    name = models.CharField(max_length=30, blank=True)
    mac = models.CharField(max_length=30, blank=True)
    class Meta:
        db_table = 'vw_vehicles_equipements'
        managed = False

最后的注释: 我无法使用uri过滤:

curl 'http://dope-apipc01/refeq/v1/vehiclesequipements/?format=json&vehicle_id__exact=9899'

我是Django和Deliciouspie的新手。非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

似乎要访问资源,它必须具有唯一的resource_uri,否则将返回异常MultipleObjectsReturned