我使用Django 1.9.6 + Tastypie来实现RESTFUL api,有一个需要从另一台服务器上的api whitch获取数据的api,我不知道该怎么做。< / p>
我发现的所有例子都是这样的:
from tastypie.resources import ModelResource
from services.models import Product
from tastypie.authorization import Authorization
class ProductResource(ModelResource):
class Meta:
queryset = Product.objects.all()
resource_name = 'product'
allowed_methods = ['get']
authorization = Authorization()
资源类从应用程序的模型(本地数据库)中获取数据,是否可以请求另一台服务器上的API?如果答案是肯定的,那么该如何做?
也许这个问题很愚蠢。
谢谢:)
答案 0 :(得分:1)
您所寻找的可能是nested Resources或仅仅是相关的资源字段,例如:
full=True
ShelfResource
会将整个ProductResource
放在 <!DOCTYPE html>
<html>
<head>
<!-- Dependencias/Elementos -->
<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../../bower_components/iron-label/iron-label.html">
<link rel="import" href="../../bower_components/iron-pages/iron-pages.html">
<link rel="import" href="../../bower_components/paper-icon-button/paper-icon-button.html">
<link rel="import" href="../../bower_components/paper-input/paper-input.html">
<link rel="import" href="../../bower_components/paper-button/paper-button.html">
<link rel="import" href="../../bower_components/paper-tabs/paper-tabs.html">
</head>
<dom-module id="protocolo-app">
<style>
:host {
display: block;
}
.card {
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 1px 5px 0 rgba(0, 0, 0, 0.12), 0 3px 1px -2px rgba(0, 0, 0, 0.2);
padding: 16px;
margin: 24px;
border-radius: 5px;
background-color: #fff;
color: #757575;
}
.inputGray{
background-color: #555;
color: #fff;
}
h1 {
font-size: 22px;
margin: 16px 0;
color: #212121;
}
paper-tabs[grayTab]{
background: #eee;
margin-top: 3%;
margin-bottom: 3%;
}
.flex {
@apply(--layout-horizontal);
}
.flex-horizontal-with-ratios {
@apply(--layout-horizontal);
}
.flexchild {
@apply(--layout-flex);
}
.flex2child {
@apply(--layout-flex-2);
}
.flex3child {
@apply(--layout-flex-3);
}
.flex-center-justified {
@apply(--layout-horizontal);
@apply(--layout-center-justified);
}
.flex-equal-justified {
@apply(--layout-horizontal);
@apply(--layout-justified);
}
.flex-equal-around-justified {
@apply(--layout-horizontal);
@apply(--layout-around-justified);
}
paper-button.gray {
background: #eee;
color: #000;
}
paper-button.gray:hover {
background: #555;
}
paper-button[disabled],
paper-button[toggles][active] {
background: red;
}
</style>
<template>
<div class="card">
<h1>Alta de Protocolos</h1>
<div class="cointainer flex-horizontal-with-ratios">
<div class="flex2child">
<iron-label>Nombre de Protocolo</iron-label>
</div>
<div class="flexchild">
<iron-label>Núm. Visitas</iron-label>
</div>
</div>
<div class="cointainer flex-horizontal-with-ratios">
<div class="flex2child">
<paper-input name="protocol" id="protocol" value="{{protocol}}"></paper-input>
</div>
<div class="flexchild">
<paper-input name="visit" id="visit" value="{{visits}}" prevent-invalid-input allowed-pattern="[0-9]" on-change="visitChanged"></paper-input>
</div>
</div>
<!-- Tab's Element-->
<paper-tabs selected="{{selected}}" scrollable no-slide noink grayTab>
<template id="tabs" is="dom-repeat" items="{{tabArray}}">
<paper-tab>{{item.value}}</paper-tab>
</template>
</paper-tabs>
<!-- /Tab's Element-->
<!-- iron-pages Element's-->
<iron-pages selected="{{selected}}">
<template id="pages" is="dom-repeat" items="{{tabArray}}">
<div>
<template id="inputs" is="dom-repeat" items="{{concat(tabArray.array, index)}}">
<div class="container flex">
<div>
<paper-input id="input" value= "{{item.value}}" placeholder="{{item.placeholder}}"></paper-input>
</div>
<div>
<paper-button class="gray" raised on-tap="delInput">Eliminar</paper-button>
</div>
</div>
</template>
<div class="container flex">
<paper-button class="gray" raised on-tap="addInput">Agregar</paper-button>
</div>
</div>
</template>
</iron-pages>
<!-- /iron-pages Element's-->
<div class="container flex-center-justified">
<div>
<paper-button class="gray" raised>Cancelar</paper-button>
<paper-button class="gray" raised>Guardar</paper-button>
</div>
</div>
</div>
</template>
</don-module>
<script>
Polymer({
is: 'protocolo-app',
properties: {
protocol: {
type: String,
value: ""
},
visits:{
type:Number,
value: 3,
notify: true
},
tabArray: {
type: Array,
value: [{value:"visita1"},{value:"visita2"},{value:"visita3"}]
},
inputArray: {
type: Array,
value: [{value:"",placeholder:"t1_input1"},{value:"",placeholder:"t1_input2"},{value:"",placeholder:"t1_input3"}]
}
},
//funcion que agrega input's
addInput: function(){
this.push('inputArray0',{value:"",placeholder:""});
},
//funcion que elimina input's
delInput: function(e){
this.splice('inputArray0',e.model.index,1);
},
creaArrayTabs: function(e){
console.log(this.value);
},
concat: function(v1, v2){
var res = v1 + v2;
alert("Concatenacion: " + res);
return v1 + v2;
},
//funcion que carga los arrays necesarios para la aplicacion
visitChanged: function(e){
//alert("Valor: " + this.$.visit.value);
//limpiamos el array para agregar las nuevas tabs
this.splice('tabArray',e.model);
//asignamos a la variable max el valor del input
var max = this.$.visit.value;
for (i = 0; i < max; i++) {
//alert("Prueba " + i);
this.push('tabArray',{value:"Visita " + i, array:"inputArray"});
this['inputArray'+ i] = [];
for (j = 0; j < 3; j++) {
this.push('inputArray' + i,{value:"",placeholder:"t"+ i + "_input"+j});
}
}
}
});
</script>
答案 1 :(得分:-1)
EDITED
实现它的一种方法可能如下所示:
import requests
from requests.exceptions import RequestException
[...]
# implementing connection in models.py gives more flexibility.
class Product(models.Model):
[...]
def get_something(self):
try:
response = requests.get('/api/v1/other/cars/12341')
except RequestException as e:
return {'success': False, 'error': 'Other service unavailable!'}
if response.status_code not in [200, 201, 202, 203, 204, 205]:
return {'success': False, 'error': 'Something went wrong ({}): {}'.format(response.status_code, response.content)}
data = json.load(response.content)
data['success'] = True
return data
def something(self):
self.get_something()
from tastypie.resources import ModelResource
from services.models import Product
from tastypie.authorization import Authorization
class ProductResource(ModelResource):
something = fields.CharField('something', readonly=True)
class Meta:
queryset = Product.objects.all()
resource_name = 'product'
allowed_methods = ['get']
authorization = Authorization()
注意该字段不会&#39;被序列化为JSON它将被转义。了解如何修复它there