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!

Recommended Answers

All 3 Replies

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"] }.

Thank you!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.