Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change settings access to support FastAPI-style single file apps #1169

Open
radiac opened this issue May 18, 2024 · 0 comments
Open

Change settings access to support FastAPI-style single file apps #1169

radiac opened this issue May 18, 2024 · 0 comments

Comments

@radiac
Copy link

radiac commented May 18, 2024

tl;dr: I would like to import ninja before settings.DEBUG is available, so I can build a Django API in a single file. Are you open to a PR to address this?

I'm the author of nanodjango, a project to write a full Django project in a single file (views, models, admin etc), and then automatically convert it into a normal project structure once it has grown. I'm aiming to make a Flask or FastAPI alternative, with the power and scalability of Django.

A key piece of this is supporting django-ninja - I love the syntax and think they fit really well together. For example, this is a working single-file django project with a model, admin, standard view and a ninja api:

from django.db import models
from nanodjango import Django
django = Django()
from ninja import NinjaAPI
api = NinjaAPI()

@django.admin
class CountLog(models.Model):
    timestamp = models.DateTimeField(auto_now_add=True)

@api.get("/add")
def add(request):
    CountLog.objects.create()

django.route("api/", include=api.urls)

@django.route("/")
def index(request):
    return f"<p>Number of API calls: {CountLog.objects.count()}</p>"

But you'll notice I've run into a problem with import order - django-ninja is testing settings.DEBUG on import (1, 2), but I have no way to configure django's settings until after nanodjango initialises, so I have to import it after that.

I'd really like to make it so that import order doesn't matter, so they can go together naturally at the top of the file and follow pep8.

Would you be open to me submitting a change to ninja.utils.is_debug_server to catch when settings aren't initialised and defer collection until settings are ready? I wouldn't change normal usage, just aim to allow people to import ninja at an unexpected
time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant