Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Returning multiple lists from one SQLAlchemy query organized by a dict attribute FLASK

I am trying to write a python function that receives a list of all VolunteerReports from a DB and then organizes them into separate lists based on their "report_category", and returns one dict with many lists inside.

my current function:

def get_volunteer_reports_for_client(volunteer_id, client_id):
    reports_obj = {}

    volunteer_reports = db_session.query(VolunteerReport).filter(
        VolunteerReport.volunteer_id == volunteer_id, \
            VolunteerReport.client_id == client_id).all()

    if not volunteer_reports:
        return []

    res = [volunteer_report_schema.dump(volunteer_report) for volunteer_report in volunteer_reports]

    for r in res:
        reports_obj[r['report_category']] = [r]

    return reports_obj

res returns a list that looks like so:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
    {
        "client_id": 24,
        "created_at": "2022-02-10 16:41:52.538363",
        "id": 1,
        "report": "This is the first report",
        "report_category": "Client Assigned",
        "volunteer_id": 23
    },
    {
        "client_id": 24,
        "created_at": "2022-02-10 16:41:52.623236",
        "id": 2,
        "report": "This is the SECOND report",
        "report_category": "Client Assigned",
        "volunteer_id": 23
    },
    {
        "client_id": 24,
        "created_at": "2022-02-10 16:41:59.493851",
        "id": 3,
        "report": "This is the THIRD report",
        "report_category": "Fulfilled The Need",
        "volunteer_id": 23
    },
    {
        "client_id": 24,
        "created_at": "2022-02-10 16:43:06.364411",
        "id": 4,
        "report": "This is the FOURTH report",
        "report_category": "Fulfilled The Need",
        "volunteer_id": 23
    }
]

reports_obj currents returns this:

{
    "Client Assigned": [
        {
            "client_id": 24,
            "created_at": "2022-02-10 16:41:52.623236",
            "id": 2,
            "report": "This is the SECOND report",
            "report_category": "Client Assigned",
            "volunteer_id": 23
        }
    ],
    "Fulfilled The Need": [
        {
            "client_id": 24,
            "created_at": "2022-02-10 16:43:06.364411",
            "id": 4,
            "report": "This is the FOURTH report",
            "report_category": "Fulfilled The Need",
            "volunteer_id": 23
        }
    ]
}

At this point, it is omitting some of the entries and only returning the last indexed entry with that "report_category". Any advice?

>Solution :

In every iteration of the loop you are overwriting the current list of dicts in that category with a new list of [r]. You need to append the current dict to the list if the key already exists and otherwise create a new list.

for r in res:
    category = r['report_category']
    if category in reports_obj:
        reports_obj[category].append(r)
    else:
        reports_obj[category] = [r]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading