This project has been archived by its maintainers, and is no longer receiving any updates.
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.
Metadata
Release files for djfapi 0.0.70
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| djfapi-0.0.70.tar.gz | 33.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| djfapi-0.0.70-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 71.2 kB
Release files / djfapi-0.0.70.tar.gz
| Download URL | djfapi-0.0.70.tar.gz |
|---|---|
| Size | 33.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6ff6c686c69160b7feed985e87479dc79bf2b27968f7abb0b5601eb7365daee4
|
|
BLAKE2b-256 checksum How to use checksums |
d52fb8e4988f81cebc00d2e7dde6df8b4ae8ba20d07a4fe55375b3b8a8ce1e18
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Release files / djfapi-0.0.70-py3-none-any.whl
| Download URL | djfapi-0.0.70-py3-none-any.whl |
|---|---|
| Size | 38.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
614e8b427ea93d809739a9303e4f568f60a1710a7ff42fed8b95dea93097a976
|
|
BLAKE2b-256 checksum How to use checksums |
b50f0334a528a9c4e4b80aa333de5e842884b3ada008e3d4c054b746e32bbd85
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|