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.3.5.tar.gz (24.5 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.3.5-py3-none-any.whl (30.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for appboot-0.3.5.tar.gz
Algorithm Hash digest
SHA256 c994869e67943d5a72e297063a6c0b5c13634e4db2d56c8e150b6adbf8ab291f
MD5 a161abaa48c05024dcf2ed2a9fc4adfe
BLAKE2b-256 031892be81caebcc6926920982da91279b39874a7367679cab3b4ba08dc5853c

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for appboot-0.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 080906dd99234fa0e7314aab36452f90901221dc3e936dcfecd497eb35204eaa
MD5 5d577fe5a2a4c6fa779c7e18f9cad32d
BLAKE2b-256 07d700afbef465d09d43dbe13cb8c443a954ebcc3a6d14d5b621584e022ee401

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