Skip to main content

Django admin for non Django ORM

Project description

solo-django-admin

Django admin for non Django ORM

For now support only Tortoise ORM

https://github.com/tortoise/tortoise-orm

Install

pip install solo-django-admin

Config

  1. Create settings.py file and redefine DATABASES if needed because by default db engine is 'django.db.backends.sqlite3'
from solo_django_admin.core.settings import *

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.getenv(f'{ENV_PREFIX}_DB_NAME'),
        'USER': os.getenv(f'{ENV_PREFIX}_DB_USER'),
        'PASSWORD': os.getenv(f'{ENV_PREFIX}_DB_PASSWORD'),
        'HOST': os.getenv(f'{ENV_PREFIX}_DB_HOST'),
        'PORT': os.getenv(f'{ENV_PREFIX}_DB_PORT', default=5432)
    }
}
INSTALLED_APPS.append('YOR_APP_NAME')
  1. Create .env and/or set environments variables if needed, all values are optional
ENV_PREFIX=DJANGO_ADMIN  # by default
DJANGO_ADMIN_DEBUG=True  # default False
DJANGO_ADMIN_DJANGO_SETTINGS_MODULE=path_to_settings_file
DJANGO_ADMIN_DB_USER=postgres
DJANGO_ADMIN_DB_NAME=postgres
DJANGO_ADMIN_DB_HOST=localhost
DJANGO_ADMIN_DB_PASSWORD=postgres
DJANGO_ADMIN_DB_PORT=5432
DJANGO_ADMIN_STATIC_URL=/django-admin-static/  # by default
DJANGO_ADMIN_STATIC_ROOT=path_to_static_files
DJANGO_ADMIN_MEDIA_URL=/django-admin-media/  # by default
DJANGO_ADMIN_MEDIA_ROOT=path_to_media_files
DJANGO_ADMIN_ALLOWED_HOSTS=* # by default (example: https://site.com,localhost)
  1. Collect static
python -m solo_django_admin.manage collectstatic --settings=path_to_settings
  1. Migrate database

Attention! the following tables will be created in the database, they are necessary for the operation of the Django admin panel

auth_group
auth_group_permissions
auth_permission
auth_user
auth_user_groups
auth_user_user_permissions
django_admin_log
django_content_type
django_migrations
django_session
  1. Create superuser
python -m solo_django_admin.manage createsuperuser
  1. Start app

Adding models to the admin panel is no different from Django, but adding models is a little different

create your own app, for example mappers

python -m solo_django_admin.manage startapp mappers

add the same models as in your application on fastapi

models.py

from solo_django_admin.models import MapperModel
from path_to_fastapi_model import Account as FastAPIAccount

class Account(MapperModel):
    fast_api_model = FastAPIAccount

admin.py

from django.contrib import admin
from .models import Account

@admin.register(Account)
class AccountAdmin(admin.ModelAdmin):
    ...

Don`t forget add your app to settings

INSTALLED_APPS.append('mappers')


How to use FK, ManyToMany, OneToOne etc. fields see examples


Example how to connect with FastAPI

import os

from fastapi import FastAPI

from solo_django_admin.core.asgi import application
from solo_django_admin.core.settings import BASE_DIR

from starlette.middleware.cors import CORSMiddleware
from starlette.staticfiles import StaticFiles

from path_to_your_lifespan import lifespan


def create_app():
    app_ = FastAPI(lifespan=lifespan)
    app_.mount(
        "/django-admin-static",
        StaticFiles(
            directory=os.path.join(BASE_DIR, "django_admin_static_files")),
        name="static",
    )
    app_.mount("/admin", application)

    app_.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],
        allow_credentials=False,
        allow_methods=["*"],
        allow_headers=["*"],
        expose_headers=["*"],
    )
    # YOUR SETTINGS HERE
    return app_


app = create_app()

if __name__ == '__main__':
    ...

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

solo_django_admin-0.1.5.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

solo_django_admin-0.1.5-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file solo_django_admin-0.1.5.tar.gz.

File metadata

  • Download URL: solo_django_admin-0.1.5.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.13.0

File hashes

Hashes for solo_django_admin-0.1.5.tar.gz
Algorithm Hash digest
SHA256 433848e386eff4e298b14ad256cc0c7c83df56948fb8b182ce3a8f7c16062d37
MD5 64c988000bc0482837e777c3dd4f275f
BLAKE2b-256 724256f31e8520d47a52783dc77961d7f8de3725df29735ce432f5b214e00458

See more details on using hashes here.

File details

Details for the file solo_django_admin-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for solo_django_admin-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 ceddf29b7b5ddc180b78e37f5a02ae755d954dad068ada2867416d11bf0cfd4e
MD5 e91358c079c089592336f7b3185ef6c9
BLAKE2b-256 7e8657320d516da17b21b2d6262d9ff1cda86eca9fd316604f776818b6a32f98

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