mirror of
https://github.com/fastapi/fastapi.git
synced 2026-02-25 19:29:45 -05:00
📝 Add Project Generation section
This commit is contained in:
@@ -28,7 +28,7 @@ Create a model for the common parameters (and don't pay attention to the rest, f
|
||||
|
||||
Now we can return a Pydantic model from the dependency ("dependable") with the same data as the dict before:
|
||||
|
||||
```Python hl_lines="18"
|
||||
```Python hl_lines="17"
|
||||
{!./src/dependencies/tutorial002.py!}
|
||||
```
|
||||
|
||||
@@ -42,7 +42,7 @@ commons: CommonQueryParams = Depends(common_parameters)
|
||||
|
||||
It won't be interpreted as a JSON request `Body` because we are using `Depends`:
|
||||
|
||||
```Python hl_lines="22"
|
||||
```Python hl_lines="21"
|
||||
{!./src/dependencies/tutorial002.py!}
|
||||
```
|
||||
|
||||
@@ -55,7 +55,7 @@ It won't be interpreted as a JSON request `Body` because we are using `Depends`:
|
||||
|
||||
And now we can use that model in our code, with all the lovable editor support:
|
||||
|
||||
```Python hl_lines="24 25 26"
|
||||
```Python hl_lines="23 24 25"
|
||||
{!./src/dependencies/tutorial002.py!}
|
||||
```
|
||||
|
||||
|
||||
@@ -10,6 +10,9 @@ You can adapt it to any other NoSQL database like:
|
||||
* **ArangoDB**
|
||||
* **ElasticSearch**, etc.
|
||||
|
||||
!!! tip
|
||||
There is an official project generator with **FastAPI** and **Couchbase**, all based on **Docker**, including a frontend and more tools: <a href="https://github.com/tiangolo/full-stack-fastapi-couchbase" target="_blank">https://github.com/tiangolo/full-stack-fastapi-couchbase</a>
|
||||
|
||||
## Import Couchbase components
|
||||
|
||||
For now, don't pay attention to the rest, only the imports:
|
||||
@@ -49,7 +52,7 @@ This utility function will:
|
||||
* Set defaults for timeouts.
|
||||
* Return it.
|
||||
|
||||
```Python hl_lines="13 14 15 16 17 18 19 20"
|
||||
```Python hl_lines="13 14 15 16 17 18 19 20 21 22"
|
||||
{!./src/nosql_databases/tutorial001.py!}
|
||||
```
|
||||
|
||||
@@ -61,7 +64,7 @@ As **Couchbase** "documents" are actually just "JSON objects", we can model them
|
||||
|
||||
First, let's create a `User` model:
|
||||
|
||||
```Python hl_lines="23 24 25 26 27"
|
||||
```Python hl_lines="25 26 27 28 29"
|
||||
{!./src/nosql_databases/tutorial001.py!}
|
||||
```
|
||||
|
||||
@@ -75,7 +78,7 @@ This will have the data that is actually stored in the database.
|
||||
|
||||
We don't create it as a subclass of Pydantic's `BaseModel` but as a subclass of our own `User`, because it will have all the attributes in `User` plus a couple more:
|
||||
|
||||
```Python hl_lines="30 31 32"
|
||||
```Python hl_lines="32 33 34"
|
||||
{!./src/nosql_databases/tutorial001.py!}
|
||||
```
|
||||
|
||||
@@ -96,7 +99,7 @@ Now create a function that will:
|
||||
|
||||
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="35 36 37 38 39 40 41"
|
||||
```Python hl_lines="37 38 39 40 41 42 43"
|
||||
{!./src/nosql_databases/tutorial001.py!}
|
||||
```
|
||||
|
||||
@@ -131,7 +134,7 @@ UserInDB(username="johndoe", hashed_password="some_hash")
|
||||
|
||||
### Create the `FastAPI` app
|
||||
|
||||
```Python hl_lines="45"
|
||||
```Python hl_lines="47"
|
||||
{!./src/nosql_databases/tutorial001.py!}
|
||||
```
|
||||
|
||||
@@ -141,7 +144,7 @@ As our code is calling Couchbase and we are not using the <a href="https://docs.
|
||||
|
||||
Also, Couchbase recommends not using a single `Bucket` object in multiple "<abbr title="A sequence of code being executed by the program, while at the same time, or at intervals, there can be others being executed too.">thread</abbr>s", so, we can get just get the bucket directly and pass it to our utility functions:
|
||||
|
||||
```Python hl_lines="48 49 50 51 52"
|
||||
```Python hl_lines="50 51 52 53 54"
|
||||
{!./src/nosql_databases/tutorial001.py!}
|
||||
```
|
||||
|
||||
|
||||
@@ -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