【问题标题】:flask add a new row based on id of other tables烧瓶根据其他表的ID添加一个新行
【发布时间】:2020-02-12 00:37:16
【问题描述】:

我有两个表,命名为项目和动作,每个项目都包含几个动作

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)

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

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

@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")

【问题讨论】:

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


    【解决方案1】:

    步骤:

    1. 更新 URL 以包含 project_id 作为路径参数:例如:/project/1/actions/add 旨在为 id 为 1 的项目加载带有新操作表单的页面
    2. 按照步骤 1 更新链接以在上一页中添加新操作(如屏幕截图所示)
    3. ActionForm 中删除project 字段,因为它是使用路径参数处理的
    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2020-08-18
      • 2020-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多