Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以Python/Flask/SQLAlchemy的形式在两个db记录之间创建关系_Python_Python 3.x_Flask_Sqlalchemy_Flask Sqlalchemy - Fatal编程技术网

以Python/Flask/SQLAlchemy的形式在两个db记录之间创建关系

以Python/Flask/SQLAlchemy的形式在两个db记录之间创建关系,python,python-3.x,flask,sqlalchemy,flask-sqlalchemy,Python,Python 3.x,Flask,Sqlalchemy,Flask Sqlalchemy,我拼命地想做以下几件事: 以烧瓶形式捕捉2个人(即父亲和母亲)的详细信息。每个个体都是单个类/模型的实例,并存储在SQLAlchemy表中 当添加第二个个体(可以是父亲或母亲)时,将在Parents表中使用这两个个体的ID创建一个关系。这将在父亲和母亲之间创建一个关系记录。我很高兴一切都安排好了 主要应用程序代码如下。我想确保我可以同时获取两个id,以创建关系(因此打印语句) 然而,我第一次添加一个单独的、两个打印语句输出: New father ID is 1 New mother ID i

我拼命地想做以下几件事:

  • 以烧瓶形式捕捉2个人(即父亲和母亲)的详细信息。每个个体都是单个类/模型的实例,并存储在SQLAlchemy表中
  • 当添加第二个个体(可以是父亲或母亲)时,将在Parents表中使用这两个个体的ID创建一个关系。这将在父亲和母亲之间创建一个关系记录。我很高兴一切都安排好了
  • 主要应用程序代码如下。我想确保我可以同时获取两个id,以创建关系(因此打印语句)

    然而,我第一次添加一个单独的、两个打印语句输出:

    New father ID is 1
    
    New mother ID is None  # this is probably expected as I haven't added the second Individual yet
    
    第二次添加个人时,我得到:

    New father ID is None
    
    New mother ID is 2      # What has happened to the first Individual's ID?
    
    我曾尝试声明全局变量以将ID写回,但在声明变量之前遇到了使用变量的错误

    有人能帮忙吗

    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)
    

    感谢@van,以下是工作代码:

        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)
    
    
    

    您是否为每位家长提出单独的表格张贴请求?如果是,则很清楚为什么不保留
    新父项
    ——它不会添加到会话中。在这种情况下,您应该将它添加到flask会话中,以便在请求之间保留它。谢谢您的回复。是的,我在父亲、母亲和孩子旁边有一个添加按钮。我对Flask比较陌生-我应该如何修改代码以将
    new\u father
    添加到会话中?那么您是否多次提交表单?每次对新的
    个人
    ?是的,没错。因此,我输入父亲的名字,点击Add并将其保存为新的个人。然后我输入母亲的名字,点击Add并将她保存为新的个人。一旦创建了第二个表(无论哪种方式),我就想在一个单独的父表中创建与它们的关系(我知道怎么做)。它只是能够检测到我作为父亲添加的个人的ID。谢谢,请参阅官方文档。您只需将以前的对象添加到
    会话中
    ,并在每个新的
    帖子上检查它们。。。一经处理,立即进行清理<代码>会话['new\u father\u id']=new\u father.id
    。并检查母亲:
    如果会话中有“新父亲id:…