Answer the question
In order to leave comments, you need to log in
How to adequately bind fields in this data model using SQLAlchemy?
Source:
flask application database
infrastructure - flask-sqlAlchemy + sqlite
features - applied unloaded application with a complex system of data models that are connected in parallel and influence each other, building a dynamic hierarchical system that exists in parallel in several hierarchical states. Relational databases were chosen.
class Attribute(db.Model):
id = db.Column(db.Integer, primary_key=True)
attributename= db.Column(db.String(120), index=True, unique=False)
source= db.Column(db.String(120), index=True, unique=False)
description = db.Column(db.Text, index=True)
class Criteria(db.Model):
id = db.Column(db.Integer, primary_key=True)
criteria= db.Column(db.String(64), index=True, unique=True)
class Classification(db.Model):
id = db.Column(db.Integer, primary_key=True)
source= db.Column(db.String(120), index=True, unique=False)
description = db.Column(db.Text, index=True)
class Node(db.Model):
id = db.Column(db.Integer, primary_key=True)
parent_id = db.Column(db.Integer, db.ForeignKey('node.id'))
attribute_id = db.Column(db.Integer, db.ForeignKey('attribute.id'))
criteria_id = db.Column(db.Integer, db.ForeignKey('criteria.id'))
classification_id = db.Column(db.Integer, db.ForeignKey('classification.id'))
left = db.Column("lft", db.Integer, index=True, nullable=False)
right = db.Column("rgt", db.Integer, index=True, nullable=False)
childs = db.relationship('Node', backref=db.backref("parent", remote_side=id),lazy="dynamic")
attribute= db.relationship('Attribute', backref='node')
criteria= db.relationship('Criteria', backref='node')
classification = db.relationship('Classification', backref='node')
class NodeForm(FlaskForm):
criteria_id = SelectField(u'Criteria', coerce=int)
submit = SubmitField('Add new')
@app.route('/add-node', methods=['GET', 'POST'])
def addnode():
criteria = Criteria.query.get(id)
form = NodeForm(request.POST, obj=criteria)
form.criteria_id.choices = [(g.id, g.criteria) for g in Criteria.query]
form = NodeForm()
if form.validate_on_submit():
criteria= Node(criteria_id=form.criteria_id.data)
db.session.add(criteria)
db.session.commit()
flash('New Node created!')
return redirect(url_for('addnode'))
return render_template('addnode.html', form=form)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question