I'm setting up a Pyramid back-end and an Angular front-end application.
An http GET request is sent from Angular to Pyramid.
In Pyramid a class (List()) is used to tranform the data (with the __json__ function).
When Pyramid responds to the GET request, using a json renderer, the data received at Angular side is not as expected. The id looks like being of type Array, which contains the value for the id...
Here is code to define the class List using the __json__ function:
class List():
id = None
def __init__(self, Id, Name, Image):
self.id = Id,
self.name = Name
def __json__(self, request):
return {
'Id':self.id,
'Name':self.name
}
The view using the json renderer for a GET request:
@view_config(route_name='lijsten', renderer='json', request_method='GET')
def lijsten_view(request):
request.matchdict['lijstID'] == 'techniek':
technieken = {
"01" : "Berliner wand" ,
"02" : "Secanspalen wand",
"03" : "Cutter Soil Mix wand",
"10" : "Avegaarpaal",
"11" : "Verbuisde Boorpaal",
"12" : "Grondverdingende Schroefpaal",
"13" : "Funderingsput",
"14" : "Micropaal"
}
jsonlist = []
for key, value in technieken.items():
tech = List(str(key), str(value), '')
jsonlist.append(tech)
return jsonlist
At the Angular side get.Technieken() is used to send the GET request:
getTechnieken(): Observable<Techniek[]> {
this.messageService.add('DataService: technieken aangevragen')
const url = `${this.restUrl}/lijsten/techniek`;
return this.http
.get<Techniek[]>(url)
.pipe(
//tap(_ => this.log('fetched lijsten on: ' + url )),
catchError(this.handleError<Techniek[]>('getTechnieken', []))
);
}
When printing the returned Array to the console, I get the following:
Array(8) [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…} ]
0: Object { Id: (1) […], Name: "Berliner wand" }
Id: Array [ "01" ]
0: "01"
length: 1
<prototype>: Array []
Name: "Berliner wand"
<prototype>: Object { … }
1: Object { Id: (1) […], Name: "Secanspalen wand" }
2: Object { Id: (1) […], Name: "Cutter Soil Mix wand" }
3: Object { Id: (1) […], Name: "Avegaarpaal" }
4: Object { Id: (1) […], Name: "Verbuisde Boorpaal" }
5: Object { Id: (1) […], Name: "Grondverdingende Schroefpaal" }
6: Object { Id: (1) […], Name: "Funderingsput" }
7: Object { Id: (1) […], Name: "Micropaal" }
I expanded the first object in the Array, where the Id is identified as an Array, it should be a string : Id : "01" ...
According to the Pyramid documentation, the expected output looks like this:
from pyramid.view import view_config
class MyObject(object):
def __init__(self, x):
self.x = x
def __json__(self, request):
return {'x':self.x}
@view_config(renderer='json')
def objects(request):
return [MyObject(1), MyObject(2)]
# the JSON value returned by ``objects`` will be:
# [{"x": 1}, {"x": 2}]
It's unclear to me what I'm doing wrong. Can somebody help?
In Python, a comma creates a tuple, the brackets are optional: for name, value in blah:
is equivalent to for (name, value) in blah:
. Also blah = 'Hello', 'there'
is the same as blah = ('Hello', 'there')
So, in your List class,
class List():
id = None
def __init__(self, Id, Name, Image):
self.id = Id,
self.name = Name
self.id is assigned a tuple with one element, the same as self.id = (id,)
, which from the JSON point of view is the same as self.id = [id]
.