Coding relationships between classes can be challenging at first! It sounds like a bunch of words thrown together - this thing knows this thing through that thing, but not that other thing. Using real-life examples can be helpful in visualizing those relationships.
For example, say you have some astronauts. Over the course of many years, these astronauts will visit many plants; one planet per mission. So each mission has one astronaut and one planet, and many planets are visited by many astronauts.
In Flask, the relationship between Astronaut and Planet is a many-to-many relationship, while the relationship between Astronaut and Mission and Planet and Mission are both one-to-many. We have three models: The mission model operates as a join table between the astronaut model and the planet model. The classes are called models because they define (or model) the relationships between your data.
So, how do we code these relationships?
I find it easiest to begin with the join table, because I am building out both of the relationships from there.
class Mission(db.Model): __tablename__ = 'missions' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String)
This is what we are starting with for our Mission class.
We know that each mission has one astronaut:
astronaut = db.relationship
db.relationship defines how two models are related to one another.
Let's connect it to the Astronaut class....
astronaut = db.relationship('Astronaut')
and now let's add the two-way relationship between the two models (Astronaut and Mission):
astronaut = db.relationship('Astronaut', back_populates="missions")
Great work! Since Mission holds both the Astronaut and Planet relationships, let's do the same with planet:
planet = db.relationship('Planet', back_populates="missions")
This is our Mission class with the relationships:
class Mission(db.Model): __tablename__ = 'missions' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String) astronaut = db.relationship('Astronaut', back_populates="missions") planet = db.relationship('Planet', back_populates="missions")
Awesome! Let's go back and take a look at our instructions: The mission model operates as a _join table between the astronaut model and the planet model._
So, we need to link Astronaut to Mission, and Planet to Mission. Let's start with Astronaut:
missions = db.relationship('Mission', back_populates="astronauts")
Missions is plural here, because an astronaut goes on a bunch of them (hopefully!).
And then Planet, which should look similar:
missions = db.relationship('Mission', back_populates="planets")
GREAT! All, together, this looks like:
class Planet(db.Model): __tablename__ = 'planets' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String) distance_from_earth = db.Column(db.Integer) nearest_star = db.Column(db.String) missions = db.relationship('Mission', back_populates="planet") class Astronaut(db.Model): __tablename__ = 'astronauts' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String) field_of_study = db.Column(db.String) missions = db.relationship('Mission', back_populates="astronaut") class Mission(db.Model): __tablename__ = 'missions' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String) astronaut = db.relationship('Astronaut', back_populates="astronauts") planet = db.relationship('Planet', back_populates="missions")
Lastly, let's add our foreign keys to our missions table. A foreign key is an integer that references an item in another database that links the two together. For example, astronaut 1's foreign key is 1 in the mission table, so every time we see the number 1 in that column, we know it applies to that astronaut!
Mission is the only class that needs foreign keys, since it is in charge of all the relationships.
class Mission(db.Model, SerializerMixin): __tablename__ = 'missions' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String) astronaut_id = db.Column(db.Integer, db.ForeignKey('astronauts.id')) planet_id = db.Column(db.Integer, db.ForeignKey('planets.id')) astronaut = db.relationship('Astronaut', back_populates="missions") planet = db.relationship('Planet', back_populates="missions") serialize_rules = ('-astronaut.missions', '-astronaut.planets')
Fantastic job! We've set up some relationships between our models. Thanks for coding!
Sources: Thank you to Flatiron School for this lab! I changed the class name 'Scientist' to 'Astronaut'.
以上是Exploring Model Relationships in Flask的详细内容。更多信息请关注PHP中文网其他相关文章!

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

本文解释了如何使用美丽的汤库来解析html。 它详细介绍了常见方法,例如find(),find_all(),select()和get_text(),以用于数据提取,处理不同的HTML结构和错误以及替代方案(SEL)

本文比较了Tensorflow和Pytorch的深度学习。 它详细介绍了所涉及的步骤:数据准备,模型构建,培训,评估和部署。 框架之间的关键差异,特别是关于计算刻度的

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

本文指导Python开发人员构建命令行界面(CLIS)。 它使用Typer,Click和ArgParse等库详细介绍,强调输入/输出处理,并促进用户友好的设计模式,以提高CLI可用性。

本文讨论了诸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和请求等流行的Python库,并详细介绍了它们在科学计算,数据分析,可视化,机器学习,网络开发和H中的用途

文章讨论了虚拟环境在Python中的作用,重点是管理项目依赖性并避免冲突。它详细介绍了他们在改善项目管理和减少依赖问题方面的创建,激活和利益。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

SublimeText3汉化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3 Linux新版
SublimeText3 Linux最新版