Code Monkey home page Code Monkey logo

Comments (4)

vitalik avatar vitalik commented on May 28, 2024

@carvalhochris

try pre fetching also artist_set

Card.objects.prefetch_related("cardhold_set", "artist_set")

from django-ninja.

carvalhochris avatar carvalhochris commented on May 28, 2024

@carvalhochris

try pre fetching also artist_set

Card.objects.prefetch_related("cardhold_set", "artist_set")

Hi @vitalik thanks for your response.

I tried that and get the following:

Traceback (most recent call last):
  File "/Users/christophercarvalho/ninja/env/lib/python3.11/site-packages/ninja/operation.py", line 282, in run
    return self._result_to_response(request, result, temporal_response)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/christophercarvalho/ninja/env/lib/python3.11/site-packages/ninja/operation.py", line 208, in _result_to_response
    validated_object = response_model.model_validate(
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/christophercarvalho/ninja/env/lib/python3.11/site-packages/pydantic/main.py", line 532, in model_validate
    return cls.__pydantic_validator__.validate_python(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for NinjaResponseSchema
response
  Error extracting attribute: SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async. [type=get_attribute_error, input_value=<DjangoGetter: <ninja.ope... object at 0x1136c23d0>>, input_type=DjangoGetter]
    For further information visit https://errors.pydantic.dev/2.7/v/get_attribute_error

from django-ninja.

vitalik avatar vitalik commented on May 28, 2024

yeah, maybe prefetch_related does not support async yet..

try:

@api.get("/get-cards", response=List[CardOut])
@decorate_view(cache_page(7 * 24 * 60 * 60))
async def list_cards(request):
    queryset = Card.objects.prefetch_related("cardhold_set", "artist_set")
    cards = await sync_to_async(list)(queryset) # force executing db query here by converting to list
    return cards

from django-ninja.

carvalhochris avatar carvalhochris commented on May 28, 2024

yeah, maybe prefetch_related does not support async yet..

try:

@api.get("/get-cards", response=List[CardOut])
@decorate_view(cache_page(7 * 24 * 60 * 60))
async def list_cards(request):
    queryset = Card.objects.prefetch_related("cardhold_set", "artist_set")
    cards = await sync_to_async(list)(queryset) # force executing db query here by converting to list
    return cards

I think you might be right, it doesn't work when I include artist_set, song, or cardhold_set in the schema

from django-ninja.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.