Skip to main content

Use FastAPI like Django

Project description

App Boot

English | 中文翻译

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

# 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'

python manage.py startapp polls

Create a Model

Define a Question model in polls/models.py.

from datetime import datetime
from sqlalchemy.orm import Mapped
from appboot import models

class Question(models.Model):
    question_text: Mapped[str]
    pub_date: Mapped[datetime]

Create a Schema

Define a QuestionSchema schema in polls/schema.py.

from appboot.schema import ModelSchema
from polls.models import Question

class QuestionSchema(ModelSchema):
    class Meta:
        model = Question

Write CRUD API

Write the CRUD API in polls/views.py.

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 QuestionSchema

router = 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.

from fastapi import APIRouter
from polls.views import router

root_router = APIRouter()
root_router.include_router(router, prefix='/polls', tags=['polls'])

Testing Our API

python manage.py runserver

We can now access our API directly through the browser at http://127.0.0.1:8000/docs/. API Documentation

Create Complex Query Schema

Create a QuestionQuerySchema for complex queries in polls/schema.py.

from typing import Optional
from appboot.params import QuerySchema
from appboot.filters import EqField, ContainsField

class 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

Try Out Examples

Go to Examples for more examples.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

appboot-0.2.9.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

appboot-0.2.9-py3-none-any.whl (30.3 kB view details)

Uploaded Python 3

File details

Details for the file appboot-0.2.9.tar.gz.

File metadata

  • Download URL: appboot-0.2.9.tar.gz
  • Upload date:
  • Size: 23.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.6 Darwin/23.6.0

File hashes

Hashes for appboot-0.2.9.tar.gz
Algorithm Hash digest
SHA256 fc8a031d0050203a6cf6028923acf7006b23e32a06b091ceca1927ef380ce108
MD5 9bc369e8329c18639570791daf13d632
BLAKE2b-256 3ebb11008e3ae8d862d75a2d9b43f42ec889a8c29c03cbf464cb1d1c002d7fa0

See more details on using hashes here.

File details

Details for the file appboot-0.2.9-py3-none-any.whl.

File metadata

  • Download URL: appboot-0.2.9-py3-none-any.whl
  • Upload date:
  • Size: 30.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.6 Darwin/23.6.0

File hashes

Hashes for appboot-0.2.9-py3-none-any.whl
Algorithm Hash digest
SHA256 1df1cae98c075daa88f003cf3ad8e43dc7c6d65f1d481ec45324379c578e6050
MD5 66037f616321c86ae41a19b8416f3b4f
BLAKE2b-256 10e0f184866c0a5b872a2e061e6027396ad3b7ed6fbbea25b781c44ec0322144

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page