Troubleshooting & FAQ¶
Common questions and issues when working with Django Ninja AIO.
Relations & Serialization¶
My reverse FK / M2M fields return empty lists
Django Ninja AIO uses prefetch_related automatically for reverse relations, but it requires:
- The related field name must be in your
ReadSerializer.fields - The related model must also be a
ModelSerializer(or have a schema defined)
class Author(ModelSerializer):
name = models.CharField(max_length=200)
class ReadSerializer:
fields = ["id", "name", "books"] # "books" = related_name on Book.author
If books is not the correct related_name, check your ForeignKey definition:
I get 'SynchronousOnlyOperation' errors with relations
This happens when Django's ORM is accessed synchronously inside an async context. Django Ninja AIO handles this automatically through prefetch_related and async iteration, but if you're writing custom code:
# Wrong - synchronous access in async view
async def my_view(request):
author = await Author.objects.aget(pk=1)
books = author.books.all() # SynchronousOnlyOperation!
# Correct - use async iteration
async def my_view(request):
author = await Author.objects.prefetch_related("books").aget(pk=1)
books = [book async for book in author.books.all()]
Tip
Let Django Ninja AIO handle relation serialization through ReadSerializer — it manages the async complexity for you.
Nested relations only show IDs, not full objects
Make sure the related model is also a ModelSerializer with its own ReadSerializer:
# This will serialize books as full objects
class Book(ModelSerializer):
title = models.CharField(max_length=200)
class ReadSerializer:
fields = ["id", "title"] # This is required!
class Author(ModelSerializer):
name = models.CharField(max_length=200)
class ReadSerializer:
fields = ["id", "name", "books"]
If Book doesn't have a ReadSerializer, the framework can't know how to serialize it.
Configuration & Setup¶
How do I disable specific CRUD operations?
Use the disable attribute on your ViewSet:
@api.viewset(Book)
class BookViewSet(APIViewSet):
disable = ["update", "delete"] # Read-only API
Available operations: "create", "list", "retrieve", "update", "delete"
How do I use a different primary key type (UUID, etc.)?
Django Ninja AIO automatically infers the PK type from your model:
import uuid
from django.db import models
class Article(ModelSerializer):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
title = models.CharField(max_length=200)
class ReadSerializer:
fields = ["id", "title"]
The generated endpoints will use UUID in the path schema automatically.
How do I change the pagination page size?
Override the pagination class on your ViewSet:
Authentication¶
How do I make some endpoints public and others protected?
Use per-method auth attributes:
@api.viewset(Book)
class BookViewSet(APIViewSet):
auth = [JWTAuth()] # Default: all endpoints require auth
get_auth = None # GET endpoints (list, retrieve) are public
Available per-method attributes: get_auth, post_auth, put_auth, patch_auth, delete_auth
I get 401 errors even with a valid token
Check these common causes:
- Token format — Ensure the header is
Authorization: Bearer <token>(notTokenorJWT) - Key mismatch — Verify your public key matches the key used to sign tokens
- Algorithm — Make sure
jwt_algmatches the signing algorithm (e.g.,RS256,HS256) - Claims — Check required claims are present in the token payload
Database & Queries¶
How do I optimize queries for large datasets?
Use QuerySet configuration on your model:
class Article(ModelSerializer):
title = models.CharField(max_length=200)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
class ReadSerializer:
fields = ["id", "title", "author"]
class QuerySet:
read = {
"select_related": ["author"],
"prefetch_related": ["tags"],
}
This ensures select_related and prefetch_related are applied automatically.
Filtering is slow on large tables
-
Add database indexes to frequently filtered fields:
-
Keep pagination enabled — never return unbounded querysets
- Limit slices for search-heavy endpoints:
queryset = queryset[:1000]
Common Errors¶
ImportError: cannot import name 'ModelSerializer'
Make sure you're importing from the correct module:
AttributeError: type object 'MyModel' has no attribute 'generate_read_s'
Your model must inherit from ModelSerializer, not Django's models.Model:
# Correct
from ninja_aio.models import ModelSerializer
class MyModel(ModelSerializer):
...
# Wrong
from django.db import models
class MyModel(models.Model):
...
If you can't change the base class, use the Meta-driven Serializer instead.
Circular import errors between models
Use string references for ForeignKey relations to models in other files:
class Book(ModelSerializer):
author = models.ForeignKey("myapp.Author", on_delete=models.CASCADE)
For serializer references, Django Ninja AIO supports string-based relation resolution.
Still Stuck?¶
-
Open an Issue
Search existing issues or create a new one
-
Browse the Docs
Full API reference and tutorials
-
Quick Start
Step-by-step getting started guide