|
|
@ -24,7 +24,7 @@ pip install fastapi |
|
|
|
|
|
|
|
Let's create a basic FastAPI application in a new file called `main.py`: |
|
|
|
|
|
|
|
=== "Python 3.8+" |
|
|
|
//// tab | Python 3.8+ |
|
|
|
|
|
|
|
```Python |
|
|
|
{!> ../../../docs_src/django_orm/tutorial001.py[ln:5,13]!} |
|
|
@ -36,7 +36,7 @@ In the next steps we'll import the `Question` model from Django, and create a Fa |
|
|
|
|
|
|
|
In your `main.py` file, let's import the `Question` model and create a FastAPI endpoint to list all questions: |
|
|
|
|
|
|
|
=== "Python 3.8+" |
|
|
|
//// tab | Python 3.8+ |
|
|
|
|
|
|
|
```Python |
|
|
|
{!> ../../../docs_src/django_orm/tutorial001.py[ln:10,16-20]!} |
|
|
@ -67,7 +67,7 @@ To fix this error, we need to configure Django settings before importing the Dja |
|
|
|
|
|
|
|
In the `main.py` add the following code **before** importing the Django models: |
|
|
|
|
|
|
|
=== "Python 3.8+" |
|
|
|
//// tab | Python 3.8+ |
|
|
|
|
|
|
|
```Python |
|
|
|
{!> ../../../docs_src/django_orm/tutorial001.py[ln:1,7-10]!} |
|
|
@ -84,7 +84,7 @@ In this guide, we learned how to use Django's ORM with FastAPI. This can be extr |
|
|
|
Django's support for async is currently limited, if you need to do run any query in an async route (or function), |
|
|
|
you need to either use the async equivalent of the query or use `sync_to_async` from `asgiref.sync` to run the query: |
|
|
|
|
|
|
|
=== "Python 3.8+" |
|
|
|
//// tab | Python 3.8+ |
|
|
|
|
|
|
|
```Python |
|
|
|
{!> ../../../docs_src/django_orm/tutorial001.py[ln:23-37]!} |
|
|
|