Skip to content
Discussion options

You must be logged in to vote

You can keep the response schema here. The dynamic part should be typed as a mapping:

from typing import Dict
from ninja import Schema

class ItemConfig(Schema):
    pass

class EnvironmentConfig(Schema):
    enabled: bool
    mapping: Dict[str, ItemConfig]

EnvironmentResponse = Dict[str, EnvironmentConfig]

@api.get("/config", response=EnvironmentResponse)
def get_config(request):
    return {
        "Environment": {
            "enabled": True,
            "mapping": {
                "Item1": {},
                "Item2": {},
            },
        }
    }

For your real data, replace "Environment" / "Item1" with generated keys.

This works because Schema is still a Pydantic model, and …

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@gotling
Comment options

@krotname
Comment options

@gotling
Comment options

Answer selected by gotling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants