vitalik / django-ninja

💨 Fast, Async-ready, Openapi, type hints based framework for building APIs
https://django-ninja.dev
MIT License
7.16k stars 426 forks source link

Documentation generation based on models #672

Open moweerkat opened 1 year ago

moweerkat commented 1 year ago

Does the django-ninja documentation generation works at runtime with models ?

I have this model with a JSONField storing a schema set by a user. This will be used to validate other json objects.

class MyModel(models.Model):
     schema = JSONField()

And items like such:

class Item(models.Model):
     model = ForeignKey()
     data = JSONField()

Example

schema = {
    "name": "foo", "type": "text", "required": True,
    "name": "price", "type": "number", "required": False
}

A view with a query param that is the name of the model will fetch the schema in the JSONField and validate the payload with.

Payload example:

payload = {
    "model": 1,
    "data" = {
        "foo": "bar",
        "price": 42
    }
}

By doing a POST on /api/{my_model}/item/ I'm fetching the specified model in the qp, I validate the data payload and create the item.

Is there a way to have as many endpoints as models in the documentation with the fields in the schema listed ?

I guess the OpenAPI file is generated when launching django but I don't know if we can alter it at runtime.

vitalik commented 1 year ago

yes OpenAPI file is generated on first access