0

I am still learning here and am currently trying to use FLASK to create to Restful interface

What I want to do is to pull out a collection of records from a mongo database. I have find_one() working fine and am now exploring how to itterate over a cursor

This code only displays a single record when I know there are at least 5

@app.route('/oer/api/v1.0/getType/', methods = ['GET'])
def getType():

    # Prepare a dictionary object to hold the result of any processing
    result = {}

    # Get hold of DB connection
    db_connection = getDbConnection()
    db = db_connection['OER']
    # Extract all records passed for the paramater that matches "Type": ie MOOC
    oerType = request.args.get("oerType");
    # test to see that there is something in the string if so try to get a record
    # Not doing this test yet lets get it working with a known record first 

    for d in db.oer_records.find({"Type":oerType}): 
        result.update(make_public_page(d))

    return jsonify(result) 

So it works but only returns a single json document not a collection? I thought the result.update would append a new record each time through FYI make_public_page() removes BISON ID's to allow jsonify to work.

This is what it returns

{
    "Account No": 1,
    "Country/ continent": "Argentina",
    "Educational Level": "Schools",
    "Educational Level (ISCED)": "2|3",
    "End year": "",
    "Funders": "",
    "Geolocation": "",
    "Initiative HQ address": "",
    "Initiative HQ city": "",
    "Initiative URL": "http://www.gleducar.org.ar",
    "Type": "OER"
}

Any help appreciated.

Thanks

1 Answer 1

2

When using dict.update(dict2) you are adding dictionary dict2's key-values pairs in to dict which result in one big dict. What you might want to do is creating a list of dicts instead.

mylist = []
mylist.append(dict)

Translated to your code:

@app.route('/oer/api/v1.0/getType/', methods = ['GET'])
def getType():

    # Prepare a dictionary object to hold the result of any processing
    result = []  # create a list instead of dict

    # Get hold of DB connection
    db_connection = getDbConnection()
    db = db_connection['OER']
    # Extract all records passed for the paramater that matches "Type": ie MOOC
    oerType = request.args.get("oerType");
    # test to see that there is something in the string if so try to get a record
    # Not doing this test yet lets get it working with a known record first 

    for d in db.oer_records.find({"Type":oerType}): 
        result.append(make_public_page(d))  # append to the list of dicts

    return jsonify(items=result) 
Sign up to request clarification or add additional context in comments.

4 Comments

Still very new to both python and flask do you mean add mylist = [] under the result ={} then mylist.append(make_public_page(d)) ?
I am getting ValueError: dictionary update sequence element #0 has length 36; 2 is required ? Thank you for your input !
@Richard change the last line to return jsonify(items=result). I forgot that jsonify cannot return arrays. make_public_page(d) returns a dict, right?
That did the trick thank you - now got to sort out a response for null seraches and how to split them into batches of 10 records but getting then to print as a set makes a big difference !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.