0

Hey-
First time posting here so hopefully I explain everything correctly.

class sendbackcurricula(app.page):
    def GET(self):
        query = models.Curricula.query.all()
        map = dict()
     	for x in query:
            x = x.to_dict()
            map[x['owner_id']] = x['title']
        return json.dumps(map);

The above code will return the last element held in the database at 'owner_id' and 'title'. The problem is I want to return all the elements for each owner_id. For example: owner_id = 'email' and holds 'Course1', 'Course2', 'Course3'. When this code is run it returns {"email": "Course3}. I tried adding map2 = dict() and adding the contents of the map to map2 while in the for loop, but this did not work.
Any help would be great!
Thanks!

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by pyTony
1

You should create a list for your owner_id:

class sendbackcurricula(app.page):
    def GET(self):
        query = models.Curricula.query.all()
        map = dict()
     	for x in query:
            x = x.to_dict()
            key, title = x['owner_id'], x['title']
            if key not in map:
                map[key] = list()
            map[key].append(title)
        return json.dumps(map)

This should return {"email": ["Course1", "Course2", "Course3"] }.

0

Also it is not very good choice to change the meaning of map function to dictionary, even it is inside function. Could give you bad habit that bites back later.

Edited by pyTony: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.