-
Consider the following snippet : """
Type error repro for ninja.
"""
from django.db import models
from django.http import HttpRequest
from ninja import Field, Router, Schema
class CatModel(models.Model):
"""
The Django model for the cat.
"""
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=255)
class CatDTO(Schema):
"""
Schema for a cat returned by the API.
"""
id: int
name: str
class CatCreateResponseDTO(Schema):
"""
A response with a list of serialized model instances.
"""
cat: CatDTO = Field(
description="The created cat.",
)
message: str = Field(description="A message that is also part of the response.")
router = Router()
@router.post(
"/cats",
summary="Create a new cat.",
response={200: CatCreateResponseDTO},
)
def create_cat(_request: HttpRequest):
# Let's pretend we've created a cat.
model_instance = CatModel.objects.get(id=1) # model_instance is of type MyModel
return 200, CatCreateResponseDTO(cat=model_instance, message="New cat created. 🐈") This works fine at runtime, but Pyright complains about the model instance being passed to the constructor of
Indeed, I guess I could do How are we supposed to pass Django models to Ninja Thanks a lot :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @GTorreil well I'm not a big fan of making EVERYTHING strictly typed... but in your case I guess you can do the following:
|
Beta Was this translation helpful? Give feedback.
Hi @GTorreil
well I'm not a big fan of making EVERYTHING strictly typed... but in your case I guess you can do the following: