From 21c792203c7627b2bb92171892d5e23c3c14c3b5 Mon Sep 17 00:00:00 2001 From: Damien De Paoli Date: Wed, 4 Nov 2020 23:48:09 +1100 Subject: [PATCH] added in publisher relationship (via book_publisher_link), removed debug, also removed ma.Nested, not needed? --- main.py | 27 +++++++++++++++++++++------ templates/books.html | 2 +- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index 5be35b5..665185c 100644 --- a/main.py +++ b/main.py @@ -17,6 +17,11 @@ book_author_link = db.Table('book_author_link', db.Model.metadata, db.Column('author_id', db.Integer, db.ForeignKey('author.id')) ) +book_publisher_link = db.Table('book_publisher_link', db.Model.metadata, + db.Column('book_id', db.Integer, db.ForeignKey('book.id')), + db.Column('publisher_id', db.Integer, db.ForeignKey('publisher.id')) +) + 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) @@ -31,9 +36,10 @@ class Book(db.Model): modified = db.Column(db.Date) author = db.relationship('Author', secondary=book_author_link) + publisher = db.relationship('Publisher', secondary=book_publisher_link) def __repr__(self): - return "".format(self.title, self.id, self.author ) + return "".format(self.title, self.id ) class Author(db.Model): id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True) @@ -43,9 +49,14 @@ class Author(db.Model): book = db.relationship('Book', secondary=book_author_link ) def __repr__(self): - return "".format(self.firstnames, self.surname, self.book) + return "".format(self.firstnames, self.surname, self.id) +class Publisher(db.Model): + id = db.Column(db.Integer, unique=True, nullable=False, primary_key=True) + name = db.Column(db.String(50), unique=False, nullable=False) + def __repr__(self): + return "".format(self.name, self.id) ### setup serializer schemas, to make returning books/authors easier class AuthorSchema(ma.SQLAlchemyAutoSchema): @@ -54,17 +65,21 @@ class AuthorSchema(ma.SQLAlchemyAutoSchema): include_relationships = True load_instance = True +class PublisherSchema(ma.SQLAlchemyAutoSchema): + class Meta: + model = Publisher + include_relationships = True + load_instance = True + class BookSchema(ma.SQLAlchemyAutoSchema): - author = ma.Nested(AuthorSchema, many=True) class Meta: model = Book include_relationships = True load_instance = True -book_schema = BookSchema() author_schema = AuthorSchema() - -print( book_schema ) +publisher_schema = PublisherSchema() +book_schema = BookSchema() ####################################### ROUTES ####################################### @app.route("/books", methods=["GET"]) diff --git a/templates/books.html b/templates/books.html index 61c260d..b99943a 100644 --- a/templates/books.html +++ b/templates/books.html @@ -23,7 +23,7 @@ {{book.title}} {{ book.author[0]['surname'] }}, {{book.author[0]['firstnames']}} - {{ book.publisher}} + {{ book.publisher[0]['name']}} {% if book.condition == "Good" %}