簡體   English   中英

如何使用一個 URI 獲取多個資源?

[英]How to get multiple ressources using one URI?

當我發出這個 curl 命令時: curl http://localhost/refeq/v1/vehiclesequipements/?format=json -s | jq. 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 和tastepie 的新手......感謝任何幫助。

似乎要訪問必須具有唯一resource_uri的資源,否則會返回異常MultipleObjectsReturned

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM