Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/taogeyt/appboot
Use FastAPI like Django
https://github.com/taogeyt/appboot
application django-like fastapi microservice pydantic sqlalchemy
Last synced: 24 days ago
JSON representation
Use FastAPI like Django
- Host: GitHub
- URL: https://github.com/taogeyt/appboot
- Owner: taogeYT
- License: apache-2.0
- Created: 2024-07-11T12:52:00.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-10-15T15:20:14.000Z (24 days ago)
- Last Synced: 2024-10-15T15:42:02.770Z (24 days ago)
- Topics: application, django-like, fastapi, microservice, pydantic, sqlalchemy
- Language: Python
- Homepage:
- Size: 1.06 MB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# App Boot
English | [中文翻译](./README.zh-CN.md)[AppBoot](https://github.com/taogeYT/appboot) is a FastAPI project template designed to provide a Django-like structure and development experience.
## Technology Stack
- Python 3.9+
- FastAPI
- SQLAlchemy 2.0+
- Pydantic
- Uvicorn
## Quick Start
### Start a New Project
```shell
# Create the project directory
mkdir mysite
cd mysite
# Create a virtual environment to isolate our package dependencies locally
python3 -m venv env
source env/bin/activate # On Windows use `env\\Scripts\\activate`
# Install appboot and aiosqlite into the virtual environment
pip install appboot aiosqlite
# Set up a new project with a single application
appboot startproject mysite . # Note the trailing '.' character
# Start the server, application running on http://127.0.0.1:8000
python manage.py runserver
```
### Start a New App 'polls'
```shell
python manage.py startapp polls
```
### Create a Model
Define a `Question` model in `polls/models.py`.
```python
from datetime import datetime
from sqlalchemy.orm import Mapped
from appboot import modelsclass Question(models.Model):
question_text: Mapped[str]
pub_date: Mapped[datetime]
```
### Create a Schema
Define a `QuestionSchema` schema in `polls/schema.py`.
```python
from appboot.schema import ModelSchema
from polls.models import Questionclass QuestionSchema(ModelSchema):
class Meta:
model = Question
```
### Write CRUD API
Write the CRUD API in `polls/views.py`.
```python
from fastapi import APIRouter, Depends
from appboot.db import create_tables
from appboot.params import QuerySchema, QueryDepends, PaginationResult
from polls.models import Question
from polls.schema import QuestionSchemarouter = APIRouter(dependencies=[Depends(create_tables)])
@router.post('/questions/', response_model=QuestionSchema)
async def create_question(question: QuestionSchema):
return await question.create()@router.get('/questions/', response_model=PaginationResult[QuestionSchema])
async def query_questions(query: QuerySchema = QueryDepends()):
return await query.query_result(Question.objects.clone())@router.get('/questions/{question_id}', response_model=QuestionSchema)
async def get_question(question_id: int):
return await Question.objects.get(question_id)@router.put('/questions/{question_id}', response_model=QuestionSchema)
async def update_question(question_id: int, question: QuestionSchema):
instance = await Question.objects.get(question_id)
return await question.update(instance)@router.delete('/questions/{question_id}', response_model=QuestionSchema)
async def delete_question(question_id: int):
instance = await Question.objects.get(question_id)
return await instance.delete()
```
### Configure API Routes
Wire up the API URLs in `mysite/urls.py`.
```python
from fastapi import APIRouter
from polls.views import routerroot_router = APIRouter()
root_router.include_router(router, prefix='/polls', tags=['polls'])
```
### Testing Our API
```shell
python manage.py runserver
```
We can now access our API directly through the browser at [http://127.0.0.1:8000/docs/](http://127.0.0.1:8000/docs/).
![API Documentation](https://github.com/taogeYT/oss/blob/main/resource/appboot/images/polls.png?raw=true)
### Create Complex Query Schema
Create a `QuestionQuerySchema` for complex queries in `polls/schema.py`.
```python
from typing import Optional
from appboot.params import QuerySchema
from appboot.filters import EqField, ContainsFieldclass QuestionQuerySchema(QuerySchema):
ids: Optional[list[int]] = EqField(None, alias='pk', columns='id') # Query questions by ID list
question_text: Optional[str] = ContainsField(None) # Fuzzy query question_text
```
Replace `QuerySchema` with `QuestionQuerySchema` in `polls/views.py`, refresh the docs in the browser, and you will see two new query parameters in the query questions API.
![Complex Query Parameters](https://github.com/taogeYT/oss/blob/main/resource/appboot/images/query.png?raw=true)
## Try Out Examples
Go to [Examples](./examples) for more examples.