mirror of
https://github.com/fastapi/fastapi.git
synced 2026-04-08 17:10:44 -04:00
📝 Add Project Generation section
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
**FastAPI** doesn't require you to use a SQL (relational) database.
|
||||
|
||||
But you can use relational database that you want.
|
||||
But you can use any relational database that you want.
|
||||
|
||||
Here we'll see an example using <a href="https://www.sqlalchemy.org/" target="_blank">SQLAlchemy</a>.
|
||||
|
||||
@@ -69,13 +69,13 @@ That way you don't have to declare them explicitly.
|
||||
|
||||
So, your models will behave very similarly to, for example, Flask-SQLAlchemy.
|
||||
|
||||
```Python hl_lines="15 16 17 18 19"
|
||||
```Python hl_lines="16 17 18 19 20"
|
||||
{!./src/sql_databases/tutorial001.py!}
|
||||
```
|
||||
|
||||
## Create the SQLAlchemy `Base` model
|
||||
|
||||
```Python hl_lines="22"
|
||||
```Python hl_lines="23"
|
||||
{!./src/sql_databases/tutorial001.py!}
|
||||
```
|
||||
|
||||
@@ -85,7 +85,7 @@ Now this is finally code specific to your app.
|
||||
|
||||
Here's a user model that will be a table in the database:
|
||||
|
||||
```Python hl_lines="25 26 27 28 29"
|
||||
```Python hl_lines="26 27 28 29 30"
|
||||
{!./src/sql_databases/tutorial001.py!}
|
||||
```
|
||||
|
||||
@@ -93,7 +93,7 @@ Here's a user model that will be a table in the database:
|
||||
|
||||
By creating a function that is only dedicated to getting your user from a `username` (or any other parameter) independent of your path operation function, you can more easily re-use it in multiple parts and also add <abbr title="Automated test, written in code, that checks if another piece of code is working correctly.">unit tests</abbr> for it:
|
||||
|
||||
```Python hl_lines="32 33"
|
||||
```Python hl_lines="33 34"
|
||||
{!./src/sql_databases/tutorial001.py!}
|
||||
```
|
||||
|
||||
@@ -103,7 +103,7 @@ Now, finally, here's the standard **FastAPI** code.
|
||||
|
||||
Create your app and path operation function:
|
||||
|
||||
```Python hl_lines="37 40 41 42 43"
|
||||
```Python hl_lines="38 41 42 43 44"
|
||||
{!./src/sql_databases/tutorial001.py!}
|
||||
```
|
||||
|
||||
@@ -131,7 +131,7 @@ user = get_user(username, db_session)
|
||||
|
||||
Then we should declare the path operation without `async def`, just with a normal `def`:
|
||||
|
||||
```Python hl_lines="41"
|
||||
```Python hl_lines="42"
|
||||
{!./src/sql_databases/tutorial001.py!}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user