About the design pattern, I recommend a DAO and Service approach, here's a good and short article about it: https://levelup.gitconnected.com/structuring-a-large-production-flask-application-7a0066a65447
About the models User/Activity relationship, I presume you have a mapped Activity model. If this is a One to One relationship, in the User model create a activityId field for the FK field and a activity relation where your ORM (I'll assume SQLAlchemy) will retrieve when user.activity is accessed.
class Activity(db.Model):
id = db.Column(db.Integer, primary_key=True)
descrption = db.Column(db.String(255))
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
activity = db.relationship(Activity)
activityId = db.Column(db.Integer, db.ForeignKey(Activity.id))
If you have a Many to Many relationship you'll have to create a third class called ActivityUser to map the relation
class ActivityUser(db.Model):
id = db.Column(db.Integer, primary_key=True)
activity = db.relationship(Activity, lazy=True)
user = db.relationship(User, lazy=True)
activityId = db.Column(db.ForeignKey(Activity))
userId = db.Column(db.ForeignKey(User))
and you can retrieve the user activities like this (again assuming you use SQLAlchemy):
ActivityUser.query.filter(ActivityUser.userId == user.id).all()
Answer from Ares on Stack OverflowBest open source Flask repos for learning design patterns?
Flask Application Factory Pattern example
I'm a remote dev and am trying to enhance my code organization. Anybody have any good flask repositories that will help teach organization and design patterns? I'd like to concentrate on large Blueprint-organized RESTful apps, but mine often get out of hand.
EDIT: here's an MVC repo that uses Blueprints and is Restful that I like: https://github.com/tojrobinson/flask-mvc
I have been struggling with circular import errors and would like to rearrange my flask application so that it uses the 'Flask Application Factory'. Is there an example that is easy to understand for a beginner. Some examples are rather big to follow. I need a tutorial or example with a basic structure like;