0

I am desperately trying to do the following:

  1. Capture details of 2 individuals in a Flaskform (i.e. a father and a mother). Each individual is an instance of an Individual class / model and stored in an SQLAlchemy table
  2. When the second individual is added (could be the father or the mother), then a relationship is created with the ids of the two Individuals, in a Parents table. This creates a relationship record between the father and the mother. I'm happy that's all set up OK.

Main app code is below. I want to make sure I can grab both id's at the same time, to create the relationship (hence the print statements).

Yet the first time I add an individual, two print statements output:

New father ID is 1

New mother ID is None  # this is probably expected as I haven't added the second Individual yet

Second time I add an individual, I get:

New father ID is None

New mother ID is 2      # What has happened to the first Individual's ID?

I have tried declaring global variables to write the ids back to but get errors about using a variable before they are declared.

Can anyone help?

from genealogy import app
from flask import render_template, session, redirect, url_for, request
from genealogy import db
from genealogy.models import Individual, Parents
from genealogy.individual.forms import AddIndividual

@app.route("/", methods=["GET", "POST"])
def index():

    form = AddIndividual()

    def fullname(first, last):
        return first + " " + last

    if request.method == "POST":

        new_father = Individual("",None,None)
        new_mother = Individual("",None,None)
        new_child = Individual("",None,None)
        new_partners = Parents(None,None)

        if request.form.get("addfather") == "Add":
            father_forenames = form.father_forenames.data
            father_surname = form.father_surname.data
            father_fullname = fullname(father_forenames, father_surname)

            new_father = Individual(father_surname, father_fullname, father_forenames)
            db.session.add(new_father)
            session["new_father.id"] = new_father.id

            db.session.commit()
            # db.session.flush()

            # if Parents.query.filter_by(father_id=new_father.id, mother_id=new_mother.id):
            #     pass
            # else:
            #     new_partners = Parents(new_father.id, new_mother.id)
            #     db.session.add(new_partners)
            #     db.session.commit()

        if request.form.get("addmother") == "Add":
            mother_forenames = form.mother_forenames.data
            mother_surname = form.mother_surname.data
            mother_fullname = fullname(mother_forenames, mother_surname)

            new_mother = Individual(mother_surname, mother_fullname, mother_forenames)
            db.session.add(new_mother)
            session["new_mother.id"] = new_mother.id

            db.session.commit()
            # db.session.flush()

            # if Parents.query.filter_by(father_id=focus_father.id, mother_id=focus_mother.id):
            #     pass
            # else:
            #     new_partners = Parents(focus_father.id, focus_mother.id)
            #     db.session.add(new_partners)
            #     db.session.commit()

        if request.form.get("addchild") == "Add":
            child_forenames = form.child_forenames.data
            child_surname = form.child_surname.data
            child_fullname = fullname(child_forenames, child_surname)

            new_child = Individual(child_surname, child_fullname, child_forenames)
            db.session.add(new_child)
            focus_person = new_child

            db.session.commit()

        print("New father ID is " + str(session["new_father.id"]))
        print("New mother ID is " + str(session["new_mother.id"]))

        return render_template("home.html", form=form)

        # return render_template("home.html", form=form, focus_father=focus_father, focus_mother=focus_mother,
        #                        focus_person=focus_person, focus_partners=focus_partners)

    return render_template("home.html", form=form)

if __name__ == "__main__":
    app.run(debug=True)
9
  • Are you making separate form POST requests for each parent? If yes, it is clear why the new_father is not preserved - it is not added to the session. In this case you should add it to the flask session to preserve it across the requests. Commented Oct 15, 2020 at 15:43
  • Thanks for the reply. Yes, I have an Add button next to the father, the mother and the child. I am relatively new to Flask - how would I amend the code to add new_father to the session? Commented Oct 15, 2020 at 16:01
  • So are you submitting the form multiple times? Each time for new Individual? Commented Oct 15, 2020 at 16:02
  • Yes that's right. So I enter the father's name, click on Add and save him as a new Individual. Then I enter the mother's name, click on Add and save her as a new Individuual. Once the second of these is created (whichever way round), I then want to create a relationship with them in a separate Parents table (which I know how to do). It's just being able to detect the ID of the Individual I've added as a father. Thanks Commented Oct 15, 2020 at 16:05
  • Please see the flask: Quick Start: Sessions official documentation. You just need to add previous objects to the session and check for them on each new POST... and clean-up once handled. session['new_father_id'] = new_father.id. And check for the mother: if 'new_father_id' in session: .... Commented Oct 15, 2020 at 16:11

1 Answer 1

1

Thanks to @van, here's the working code:

    from genealogy import app
    from flask import render_template, session, redirect, url_for, request
    from genealogy import db
    from genealogy.models import Individual, Parents
    from genealogy.individual.forms import AddIndividual
    
    @app.route("/", methods=["GET", "POST"])
    def index():
    
        form = AddIndividual()
    
        def fullname(first, last):
            return first + " " + last
    
        if request.method == "POST":
    
            new_father = Individual("",None,None)
            new_mother = Individual("",None,None)
            new_child = Individual("",None,None)
            new_partners = Parents(None,None)
    
            if request.form.get("addfather") == "Add":
                father_forenames = form.father_forenames.data
                father_surname = form.father_surname.data
                father_fullname = fullname(father_forenames, father_surname)
    
                new_father = Individual(father_surname, father_fullname, father_forenames)
                db.session.add(new_father)

    
                db.session.commit()
                db.session.flush()
                session["new_father.id"] = new_father.id

    
            if request.form.get("addmother") == "Add":
                mother_forenames = form.mother_forenames.data
                mother_surname = form.mother_surname.data
                mother_fullname = fullname(mother_forenames, mother_surname)
    
                new_mother = Individual(mother_surname, mother_fullname, mother_forenames)
                db.session.add(new_mother)

    
                db.session.commit()
                db.session.flush()
                session["new_mother.id"] = new_mother.id
    
    
            if request.form.get("addchild") == "Add":
                child_forenames = form.child_forenames.data
                child_surname = form.child_surname.data
                child_fullname = fullname(child_forenames, child_surname)
    
                new_child = Individual(child_surname, child_fullname, child_forenames)
                db.session.add(new_child)
                focus_person = new_child
    
                db.session.commit()
    
            print("New father ID is " + str(session["new_father.id"]))
            print("New mother ID is " + str(session["new_mother.id"]))
    
            return render_template("home.html", form=form)
    
            # return render_template("home.html", form=form, focus_father=focus_father, focus_mother=focus_mother,
            #                        focus_person=focus_person, focus_partners=focus_partners)
    
        return render_template("home.html", form=form)
    
    if __name__ == "__main__":
        app.run(debug=True)


Sign up to request clarification or add additional context in comments.

Comments

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.