okay, using ORM, the book_sub_book_link table is actually linked, it is also being used to force the ordering of the quert - although its probably not needed, as the datatables sort can do it by using sub_book_num and parent_id... Those values are still being put in by hand, so need to think about the parent_id bit... we need it to get the sorting to work. I think the sub_book_num would now be there in the normal book.dump() if I try to put it there -- would need to make sure the appropriate Schema is used of course
This commit is contained in:
25
main.py
25
main.py
@@ -27,6 +27,15 @@ book_genre_link = db.Table('book_genre_link', db.Model.metadata,
|
||||
db.Column('genre_id', db.Integer, db.ForeignKey('genre_lst.id'))
|
||||
)
|
||||
|
||||
class Book_Sub_Book_Link(db.Model):
|
||||
__tablename__ = "book_sub_book_link"
|
||||
book_id = db.Column(db.Integer, db.ForeignKey('book.id'), unique=True, nullable=False, primary_key=True)
|
||||
sub_book_id = db.Column(db.Integer, db.ForeignKey('book.id'), unique=True, nullable=False, primary_key=True)
|
||||
sub_book_num = db.Column(db.Integer)
|
||||
|
||||
def __repr__(self):
|
||||
return "<book_id: {}, sub_book_id: {}, sub_book_num: {}".format(self.book_id, self.sub_book_id, self.sub_book_num)
|
||||
|
||||
class Book(db.Model):
|
||||
id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True)
|
||||
title = db.Column(db.String(100), unique=True, nullable=False)
|
||||
@@ -43,6 +52,7 @@ class Book(db.Model):
|
||||
author = db.relationship('Author', secondary=book_author_link)
|
||||
publisher = db.relationship('Publisher', secondary=book_publisher_link)
|
||||
genre = db.relationship('Genre_Lst', secondary=book_genre_link )
|
||||
subs = db.relationship('Book_Sub_Book_Link', secondary=Book_Sub_Book_Link.__table__, primaryjoin="Book.id==Book_Sub_Book_Link.sub_book_id", secondaryjoin="Book.id==Book_Sub_Book_Link.book_id" )
|
||||
|
||||
def __repr__(self):
|
||||
return "<id: {}, author: {}, title: {}, year_published: {}, rating: {}, condition: {}, owned: {}, covertype: {}, notes: {}, blurb: {}, created: {}, modified: {}, publisher: {}>".format(self.id, self.author, self.title, self.year_published, self.rating, self.condition, self.owned, self.covertype, self.notes, self.blurb, self.created, self.modified, self.publisher )
|
||||
@@ -89,10 +99,17 @@ class Genre_LstSchema(ma.SQLAlchemyAutoSchema):
|
||||
include_relationships = True
|
||||
load_instance = True
|
||||
|
||||
class Book_Sub_Book_LinkSchema(ma.SQLAlchemyAutoSchema):
|
||||
class Meta:
|
||||
model = Book_Sub_Book_Link
|
||||
include_relationships = True
|
||||
load_instance = True
|
||||
|
||||
class BookSchema(ma.SQLAlchemyAutoSchema):
|
||||
author = ma.Nested(AuthorSchema, many=True)
|
||||
publisher = ma.Nested(PublisherSchema, many=True)
|
||||
genre = ma.Nested(Genre_LstSchema, many=True)
|
||||
subs = ma.Nested(Book_Sub_Book_LinkSchema, many=True)
|
||||
class Meta:
|
||||
model = Book
|
||||
include_relationships = True
|
||||
@@ -107,7 +124,10 @@ def books():
|
||||
if request.form:
|
||||
print(request.form)
|
||||
|
||||
books = Book.query.all()
|
||||
### DDP: this fails... also, maybe we use ORM to build a parent_book (and I am child #4) and child_books???
|
||||
# books = Book.query.all()
|
||||
books = Book.query.outerjoin(Book_Sub_Book_Link, Book.id==Book_Sub_Book_Link.book_id).order_by(Book.id, Book_Sub_Book_Link.sub_book_num).all()
|
||||
|
||||
# want to get sub book info and patch it into the books object to at least reference sub_book_num and parent_book,
|
||||
# then per book in jinja2, slide it into the right aprt of the table with the right markup to show its a sub book
|
||||
subs = db.engine.execute ( "select * from book_sub_book_link" )
|
||||
@@ -137,6 +157,9 @@ def book(id):
|
||||
|
||||
book_s['sub_book'] = sub_book
|
||||
|
||||
print( book.genre )
|
||||
print( book.subs )
|
||||
|
||||
return render_template("books.html", books=book_s, subs=sub_book )
|
||||
|
||||
@app.route("/authors", methods=["GET"])
|
||||
|
||||
Reference in New Issue
Block a user