Skip to main content

Utilities for use with FastAPI and django

Project description

djfapi

A utility library to integrate and use fastapi with the django orm.

🚧 This project is WIP and is subject to change at any time

This project is currently in the alpha state, even though it can be used in production with some caution. Make sure to fix the version in your requirements.txt and review changes frequently.

Installation

pip install djfapi

Features

Sentry

Provides optional sentry integration.

djdantic usage

See djdantic for schema usage.

Automatic Route Generation

Declare routes without having to write routes using the djfapi.routing.django.DjangoRouterSchema.

The DjangoRouterSchema will supply a router, which just has to be included in the FastAPI app.

Example for Routes

from djfapi.routing.django import DjangoRouterSchema, SecurityScopes


transaction_token = JWTToken(scheme_name="Transaction Token")


def company_objects_filter(access: Access) -> Q:
    q = Q(tenant_id=access.tenant_id)
    if access.scope.selector != 'any':
        q &= Q(employees__user_id=access.user_id, employees__type__in=[models.Employee.EmployeeType.OWNER, models.Employee.EmployeeType.ADMIN])

    return q


router = DjangoRouterSchema(
    name='companies',
    model=models.Company,
    get=schemas.response.Company,
    create=schemas.request.CompanyCreate,
    update=schemas.request.CompanyUpdate,
    security=transaction_token,
    security_scopes=SecurityScopes(
        get=['business.companies.read.any', 'business.companies.read.own',],
        post=['business.companies.create',],
        patch=['business.companies.update.any', 'business.companies.update.own',],
        put=['business.companies.update.any', 'business.companies.update.own',],
        delete=['business.companies.delete.any', 'business.companies.delete.own',],
    ),
    objects_filter=company_objects_filter,
    children=[
        DjangoRouterSchema(
            name='departments',
            model=models.CompanyDepartment,
            get=schemas.response.CompanyDepartment,
            create=schemas.request.CompanyDepartmentCreate,
            update=schemas.request.CompanyDepartmentUpdate,
            children=[
                DjangoRouterSchema(
                    name='teams',
                    model=models.CompanyDepartmentTeam,
                    get=schemas.response.CompanyDepartmentTeam,
                    create=schemas.request.CompanyDepartmentTeamCreate,
                    update=schemas.request.CompanyDepartmentTeamUpdate,
                ),
            ]
        ),
        DjangoRouterSchema(
            name='costcenters',
            model=models.CompanyCostcenter,
            get=schemas.response.CompanyCostcenter,
            create=schemas.request.CompanyCostcenterCreate,
            update=schemas.request.CompanyCostcenterUpdate,
        ),
    ]
)

Info for using FastAPI and Django

⚠️ Database Connection Leaks

When using FastAPI with Django and performing django db operations from within a FastAPI Endpoint, required signals regarding request handling from django are not called. The Django signals django.core.signals.request_started and django.core.signals.request_finished need to be called prior and after every request (which uses the django orm in it's endpoint), otherwise database connections are leaked undefinedly. Those signals must be called from the same thread, in which the orm is used.

There is a method decorator provided by djfapi in djfapi.utils.fastapi_django.request_signalling, which can be applied to any (sync) endpoints. For autogenerated endpoints or manual endpoints decorated with a DjangoRouterSchema.endpoint, this is already done.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

djfapi-0.0.72b26.tar.gz (35.2 kB view details)

Uploaded Source

Built Distribution

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

djfapi-0.0.72b26-py3-none-any.whl (40.1 kB view details)

Uploaded Python 3

File details

Details for the file djfapi-0.0.72b26.tar.gz.

File metadata

  • Download URL: djfapi-0.0.72b26.tar.gz
  • Upload date:
  • Size: 35.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for djfapi-0.0.72b26.tar.gz
Algorithm Hash digest
SHA256 751db22b466dc663c8556332f636585db3ccd40fa41a1fc628e5c3337fd21de2
MD5 30e4f20c9f18c6d712a9eea66d55e9f3
BLAKE2b-256 8960c5e159189588ef99ba8e0b484b2819e9c4acdfbb859ba5cd7b308c7d4d1a

See more details on using hashes here.

Provenance

The following attestation bundles were made for djfapi-0.0.72b26.tar.gz:

Publisher: pypi_publish.yml on Voltane-EU/djfapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file djfapi-0.0.72b26-py3-none-any.whl.

File metadata

  • Download URL: djfapi-0.0.72b26-py3-none-any.whl
  • Upload date:
  • Size: 40.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for djfapi-0.0.72b26-py3-none-any.whl
Algorithm Hash digest
SHA256 07f0e11534b429038c1e77e7281a389c1e02437c013b7b38997186b451ac65ae
MD5 abcf5514907f1e7909f61d088c73ad80
BLAKE2b-256 64e16cb9de5a3350022f830ea79b752cac01632a9919f428c6082b195dbdf9c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for djfapi-0.0.72b26-py3-none-any.whl:

Publisher: pypi_publish.yml on Voltane-EU/djfapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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