Skip to content

WHOIGit/django-ninja-dto-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Data Transfer Objects with Django Ninja

Data Transfer Objects (DTOs) is an object-oriented design pattern for encapsulating data in a layer that handles input / output concerns such as serialization and validation.

In Django Ninja, DTOs can be implemented as pydantic models, which Ninja uses via type hinting to perform automatic serialization, deserialization, and validation. A service layer then enables these DTOs to be read from and written to the backend.

The example exposes a CRUD API for a simple model called Widget, showing how DTOs, services, and views can be composed.

Example request bodies are provided:

  • create-widget.json - POST to /api/widget with this request body to create your first widget
  • update-widget.json - PUT to /api/widget/{id} with this request body to update a widget
  • bad-create-widget.json - POST to /api/widget with this request body to receive a validation error

Example validation error output

Here's the validation error detail you'll receive when POSTing bad-create-widget.json to /api/widget:

{
    "detail": [
        {
            "type": "missing",
            "loc": [
                "body",
                "input",
                "name"
            ],
            "msg": "Field required"
        },
        {
            "type": "string_type",
            "loc": [
                "body",
                "input",
                "description"
            ],
            "msg": "Input should be a valid string"
        }
    ]
}

Using the client

  1. Install requirements:
    pip install -r client-requirements.txt
    
  2. Acquire an authentication token using Django admin.
  3. Copy dotenv.template to .env and replace the example token with your real token
  4. Run the client:
    python client.py
    

The client exercises all endpoints and shows status codes and outputs for each one.

About

Example of using Data Transfer Objects with Django Ninja

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published