flask根据其他表的id添加新行

时间:2019-10-15 10:45:16

标签: python flask sqlalchemy flask-sqlalchemy flask-wtforms

我有两个表,分别是项目和动作,每个项目都包含多个动作

class Projet(db.Model):
    __tablename__ = 'projets'
    id = db.Column(db.Integer, primary_key=True)
    nom_projet = db.Column(db.String(100))    
    description_projet = db.Column(db.String(800))                         
    date_affectation = db.Column(db.DateTime, nullable = False)                              
    statut_projet = db.Column(db.String(100))

    admin_id = db.Column(db.Integer, db.ForeignKey('admins.id'))

    actions = db.relationship('Action', backref='projet',
                                lazy='dynamic')

    def __repr__(self):
        return '<Projet: {}>'.format(self.id)

class Action(db.Model):
    __tablename__ = 'actions'  
    id = db.Column(db.Integer, primary_key=True)
    projet_id = db.Column(db.Integer, db.ForeignKey('projets.id'))   
    description =  db.Column(db.String(1000))                        
    statut_action = db.Column(db.String(100))                               
    date_action =  db.Column(db.DateTime, nullable = False) 
    date_execution = db.Column(db.DateTime, nullable = True) 
    def __repr__(self):
        return '<Action: {}>'.format(self.id)

我的问题是,我需要根据现有项目创建一个新动作,如图所示, enter image description here 我需要单击添加按钮,他必须将我重定向到具有自动选择的项目名称的操作表单,然后输入操作详细信息。

这是我第一个添加操作的代码:

@auth.route('/action/add', methods=['GET', 'POST'])
@login_required
def add_action():
    form = ActionForm()
    if form.validate_on_submit():
        action = Action(
            projet = form.projet.data,
            description = form.description.data,
            statut_action = form.statut_action.data,
            date_action = form.date_action.data,
            date_execution = form.date_execution.data
        )
        try:
            db.session.add(action)
            db.session.commit()
            flash('You have successfully added a new action.')
        except:
            flash('Error: action name already exists.')
        return redirect(url_for('auth.list_projets'))
    return render_template('admin/actions/action.html', action="Add", form=form,
                           title="ADD ACTION")

1 个答案:

答案 0 :(得分:0)

步骤:

  1. 更新URL以将project_id包含为路径参数:例如:/project/1/actions/add旨在为ID为1的项目加载带有新操作表单的页面
  2. 按照步骤1更新链接以在上一页中添加新操作(如屏幕截图所示)
  3. 使用路径参数处理时,从project中删除ActionForm字段
  4. 更新“新操作表单”页面,以显示product_name变量中出现的产品名称

尝试

@auth.route('/project/<project_id>/action/add', methods=['GET', 'POST'])
@login_required
def add_action(project_id):
    form = ActionForm()
    project = Project.query.get(project_id)
    if not project:
        flash('Error: invalid project')
        abort(404)
    if form.validate_on_submit():
        action = Action(
            project = project,
            description = form.description.data,
            statut_action = form.statut_action.data,
            date_action = form.date_action.data,
            date_execution = form.date_execution.data
        )
        try:
            db.session.add(action)
            db.session.commit()
            flash('You have successfully added a new action.')
        except:
            flash('Error: action name already exists.')
        return redirect(url_for('auth.list_projets'))
    return render_template('admin/actions/action.html', action="Add", form=form,
                           title="ADD ACTION", project_name=project.name)