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

[BUG] Data is not present on GET request after successful POST request. #1411

Open
Jacob1507 opened this issue Feb 17, 2025 · 1 comment
Open

Comments

@Jacob1507
Copy link

Jacob1507 commented Feb 17, 2025

Hi, I wanted to use Django Ninja on my production server. While working locally I noticed that most of the requests were taking literally minutes to be processed by API and when doing GET requests nothing was displayed. Like server was processing info whole time. It was really confusing because I was searching bugs in my apps logic, but even trivial functions were having issues. Interesting thing. While making queries in Django shell I was getting immediate query results after made POST requests, but when doing GET requests which were using same functions nothing was present.

I thought this happened, because I exposed my server on 0.0.0.0:8000 (After I rewrote app to DRF I had similar issues when running locally) and server was taking some time to resolve IP's. I decided to make unfinished deploy on dev server but same thing was happening. Now, when app is rewritten to DRF everything works as expected. Same queries and same config for Gunicorn and Nginx.

I used PostgreSQL 100% of the time. When looking for issues I tried to use SQLite, but nothing changed. Also tried to drop Docker and use virtual env. Same thing.

Sadly this issue didn't raise any errors

Example:

## logic.py

def get_user_banks_qs(user: User) -> QuerySet[BankAlias]:
    return BankAlias.objects.filter(user=user).values("id", "name").order_by("name")


def add_new_bank_alias(*, user: User, name: str) -> BankAlias:
    """Create new bank alias for user"""
    bank_alias, _ = BankAlias.objects.get_or_create(user=user, name=name)
    return bank_alias

## api.py

from ninja.pagination import paginate, PageNumberPagination
from ninja_jwt.authentication import JWTAuth

router = Router(auth=JWTAuth())


@router.get(
    "/bank-aliases/", response=list[UserBankAliasSchema], url_name="bank_aliases_list"
)
@paginate(PageNumberPagination, page_size=10)
def get_user_bank_aliases(request):
    return fin_logic.get_user_banks_qs(request.auth)


@router.post("/banks-aliases/create/", url_name="bank_alias_create")
def create_user_bank_alias(request, data: CreateUserBankAliasSchema):
    fin_logic.add_new_bank_alias(user=request.auth, name=data.name)
    return 200, dict(success=True)
## <app>/urls.py

from ninja import NinjaAPI
from ninja.throttling import AnonRateThrottle, AuthRateThrottle
from ninja_extra import exceptions
from ninja_jwt.routers.obtain import obtain_pair_router
from ninja_jwt.routers.verify import verify_router

from finances.api import router as managers_router


api = NinjaAPI(
    title="API",
    version="1",
    throttle=[
        AnonRateThrottle("10/s"),
        AuthRateThrottle("100/s"),
    ],
)


def api_exception_handler(request, exc):
    headers = {}
    if isinstance(exc.detail, (list, dict)):
        data = exc.detail
    else:
        data = {"detail": exc.detail}
    response = api.create_response(request, data, status=exc.status_code)
    for k, v in headers.items():
        response.setdefault(k, v)
    return response


api.exception_handler(exceptions.APIException)(api_exception_handler)
api.add_router("/token", tags=["Auth"], router=obtain_pair_router)
api.add_router("/token", tags=["Auth"], router=verify_router)
api.add_router("/user", managers_router)
api.add_router("/", register_router)
urlpatterns = [path("admin/", admin.site.urls), path("api/", api.urls, name="api")]

Dev:

  • Python version: 3.10.12
  • Django version: 5.1 (also 4.2)
  • Django-Ninja: 1.3.0
  • Django-Ninja JWT: 5.3.5
  • Django-Ninja Extra: 0.22.0
  • Pydantic: 2.10.4

Server:

  • Ubuntu 24.04.1 LTS
  • gunicorn: 23.0.0
  • nginx: 1.24.0
@vitalik
Copy link
Owner

vitalik commented Feb 18, 2025

well I do not think anyone can help you find the issue - you need to at least put some time prints to understand which part of you code does the bottleneck - maybe it's net, maybe database, may be something else

also would be good to know how many records are in your database (BankAlias model specifically) and if it has any indexes

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

2 participants