WTForms-SQLAlchemy一对多关系子条目未更新

时间:2018-10-23 00:41:02

标签: python-3.x flask-sqlalchemy flask-wtforms

创建表单以插入和更新具有一对多关系的SQLAlchemy数据库条目时遇到问题。在数据库中,子条目不会更新。每次提交时,“ GDPR”中都会创建一个新条目。这是代码:

from flask_wtf import FlaskForm
from wtforms import StringField, validators
from wtforms.validators import DataRequired
from wtforms.fields import FormField
from wtforms_alchemy import ModelForm, ModelFieldList

from flask_sqlalchemy import SQLAlchemy

from flask import Flask, render_template, url_for, redirect, request
app = Flask(__name__)

app.secret_key = 'top_secret'

# SQLAlchemy 
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///C:\\Users\\OMISTAJA\\Documents\\dev\\python\\sandbox\\WTF_SQL\\db\\user.db'

db = SQLAlchemy(app)


#[Start Model]
class Customer(db.Model):
    __tablename__ = 'customer'

    id = db.Column(db.Integer, primary_key=True)
    created_on = db.Column(db.DateTime, server_default=db.func.now())
    updated_on = db.Column(db.DateTime, server_default=db.func.now(), server_onupdate=db.func.now())
    lastname = db.Column(db.String(56))
    firstnames = db.Column(db.String(56)) 
    hid = db.Column(db.String(16))
    phone = db.Column(db.String(16))
    email = db.Column(db.String(56))
    addressI = db.Column(db.String(56))
    addressII = db.Column(db.String(56))
    city = db.Column(db.String(56))
    zip_code = db.Column(db.Integer)
    gdprs = db.relationship('GDPR')

class GDPR(db.Model):
    __tablename__ = 'gdpr'

    id = db.Column(db.Integer, primary_key=True)
    is_active = db.Column('active', db.Boolean )
    parent_id = db.Column(db.Integer, db.ForeignKey('customer.id'))
    startdate = db.Column('start', db.DateTime, nullable=False)   
    enddate = db.Column('end', db.DateTime, nullable=True) # how long should be ??
    desc = db.Column('info', db.String(160))

class GDPRForm(ModelForm):
    class Meta:
        model = GDPR

class CustomerForm(ModelForm):
    class Meta:
        model = Customer

    gdprs = ModelFieldList(FormField(GDPRForm))
#[END Model]

@app.route('/customer/', methods=('GET', 'POST'))
def customer():
    myobj = Customer(gdprs=[GDPR()])
    form = CustomerForm(request.form, obj=myobj)
    if request.method == 'POST':
        form.populate_obj(myobj)
        db.session.add(myobj)
        db.session.commit()
        docid=myobj.id
        print(docid)
        return redirect(url_for('edit_customer', docid=docid))
    backref = ('customer') 
    return render_template('customer.html', form=form, backref=backref)    

@app.route('/customer/<docid>', methods=('GET', 'POST'))
def edit_customer(docid):
    myobj = Customer.query.filter_by(id=docid).one()
    form = CustomerForm(request.form, obj=myobj)
    if request.method == 'POST':
        form.populate_obj(myobj)
        db.session.commit()
        docid=myobj.id
        print(docid)
    backref = ('edit_customer, ', docid)    
    return render_template('customer.html', form=form, backref=backref)    

@app.route("/reload_db")
def reload_db():
    db.drop_all()
    db.create_all() 
    return ("Data Base re-done")

if __name__ == '__main__':
    app.run(debug=True)

当我通过调用“ / customer /”路由创建数据库条目时,该条目被创建到数据库中的两个表中。 “ / customer /”中的查询工作正常,但是当我单击“提交”时,“ GDPR”表中的行未更新。每次都会创建一个新行。如果我提交了3次,则“客户”表中只有一行,而“ GDPR”中只有三行链接到“客户”表。

0 个答案:

没有答案