[Python] Add data in loop with db.commit()

Discussão em 'Python' iniciado por Stack, Junho 28, 2025.

  1. Stack

    Stack Membro Participativo

    I need help with the following Python code

    copy_groups = Group()
    groups = Group.query.filter(Group.project_id == curent_project_id)
    for group in groups:
    copy_groups.project_id = curent_project.id
    copy_groups.name = group.name
    db.session.add(copy_groups)
    db.session.commit()


    When I used this codem it was added only last group, but I need add all group in groups.

    When I used

    copy_groups = Group()
    groups = Group.query.filter(Group.project_id == curent_project_id)
    for group in groups:
    copy_groups.project_id = curent_project.id
    copy_groups.name = group.name
    db.session.add(copy_groups)
    db.session.commit()


    I had error.

    But I need add all group in groups.

    Continue reading...

Compartilhe esta Página