[Flask] flask tables with relationship tables.
mhysnm1964 at gmail.com
mhysnm1964 at gmail.com
Mon Aug 12 08:45:32 EDT 2019
All,
I am using flask_tables and flask_sqlalchemy to generate the database. I
want to know how I can populate the class you have to create for
flask_tables with an relational database. The model I am using is:
class Categories(db.Model):
id = db.Column(db.Integer, primary_key=True)
category = db.Column(db.String(64), index=True, unique=True)
subcategories = db.relationship('SubCategories', backref='categories',
lazy='dynamic')
def __repr__(self):
return '<Categories {}>'.format(self.category)
class SubCategories(db.Model):
id = db.Column(db.Integer, primary_key=True)
subcategory = db.Column(db.String(80), index=True, unique=True)
category_id = db.Column(db.Integer, db.ForeignKey('categories.id'))
transactions = db.relationship('Transactions',
backref='sub_categories', lazy='dynamic')
def __repr__(self):
return '<SubCategories {}>'.format(self.subcategory)
class Transactions (db.Model):
id = db.Column(db.Integer, primary_key=True)
transactiondate = db.Column(db.Date, index=True, nullable=False)
description = db.Column(db.String(80), nullable=False)
amount = db.Column(db.Float, nullable=False)
subcategory_id = db.Column(db.Integer,
db.ForeignKey('sub_categories.id'))
account_no = db.Column(db.Integer,
db.ForeignKey('accounts.account_number'))
The queries I have used are:
Records = Transactions.query.all()
Records = Transactions.query.join(SubCategories).join(Categories).all()
If I use records[0].SubCategories.Categories.category in the flask shell. I
get the category value. But I get nothing when I use the flask_table.
Col(SubCategories.Categories.category) in the table class. Not sure what I
am doing wrong. As I cannot find any examples for this module with
relational databases. I am assuming everything between the Col(.) is the
field of the database.
Does anyone have examples that I can have a look at using this module? AS it
appears it has a built in sort method for the table. The other question
flask_sqlalchemy has the ability of pagination. How can I get this to work
with Flask_table? Do I just feed it a sqlalchemy object that is generated
from the pagination process.
Any help or tips would be welcomed. New at all this and still trying to get
my head around everything. I do have many more questions. But they can wait
for now.
Note, Either method of query I have shown generates the same result when I
use it in the flask shell. def __repr__(self):
return '<Transactions {}>'.format(self.description)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20190812/0a2703f5/attachment.html>
More information about the Flask
mailing list