New to flask and ORM technology is doing a sample project for me to learn
My In the application, the trainer can teach one or more techniques, so I define my tables as tilt
- trainer:
- id < Li> Name
- Technology:
- ID
- Tech_name <
- Ol>
and
What do you want, many-to-many relationships are flask-SQLLame Documentation provides a
giving you the right idea about providing a table, but you do not really need the
id column in it, the example below is for your model Has happened.
technologies = db.Table ('technologies', db column (' trainer_id ', db.Integer, db.ForeignKey (' trainer.id ')), Db.Column (' tech_id ', Db.Integer, db.ForeignKey (' tech.id ')) Class Trainer (DB Model): id = db.Column (db.Integer, primary_key = true) name = db Column (db string) phone = db Column (db string) email = db Columns (DB String) Technologies = DB Relationship ('Tech', Secondary = Technologies, Backfruit = DB. Backf ('Trainers', Lazy = 'Dynamic')) Class Tech (DB Model): id = db.Column (db.Integer, primary_key = true) Name = Db.Column (db.String)
Now you can add trainer and techas, then allocate them as needed:
nano_tech = tech (Name = 'nano') mega_tech = tech (name = 'mega') bob = trainer (name = 'bob', email = 'whatever', technologies = [nano_technology, mega_tech])
Or can you add existing techniques to existing trainer
trainer = trainer.query.filter_by (name = 'Alice') first () technique = Tech.query.filter_by (name = 'super'). First () trainer.technologies.append (tech)
or any combination thereof.
No comments:
Post a Comment