django-ninja
django-ninja copied to clipboard
[proposal] need DATETIME_FORMAT to ninja Schema
maybe I think that Datetime format is useful feature like drf settings (https://www.django-rest-framework.org/api-guide/settings/#date-and-time-formatting)
proposal 1 with Pydantic Config
api =NinjaAPI(
...
renderer= ....
...
config= Config(
DATETIME_FORMAT = "%Y-%M-%D HH:MM:SS",
DATE_FORMAT = "%Y-%M-%D",
TIME_FORMAT = "%Y-%M-%D HH:MM:SS",
)
)
or. it can use it using django config.
# settings.py
DJANGO_NINJA = {
....
"DATETIME_FORMAT": "%Y-%M-%D HH:MM:SS",
"DATE_FORMAT": "%Y-%M-%D",
"TIME_FORMAT": "%Y-%M-%D HH:MM:SS",
}
if you think it's necessary, can i pull request and request review?
@KimSoungRyoul, so I have contributed to quite a few open source projects over the years, and they have all welcomed pull requests. The standard necessary to have a pull request accepted has varied with the project, and with the scope of the pull request itself.
I am only a contributor here, but I for one would love to see how you would do this sort of thing at the framework (django ninja) level or at the pydantic level, or at what combination of the two.
Hi @KimSoungRyoul Well the json representation layer is fully depends on pydantic...
you can do something like this:
class MyCustomSchema(BaseModel):
class Config:
json_encoders = {
datetime: lambda v: v.timestamp(),
}
and then reuse this schema (as base class) across the application
but yeah.. this question pops up pretty often... maybe it worth at least some documentation section
Well the json representation layer is fully depends on pydantic...
yeah fastapi is fully depends on pydantic
but It seems like a good idea to provide django-ninja's features using pydantic config.
ninja/schema.Schema & pydantic BaseModel
What do you think about managing these two classes differently?
ninja/schema.Schema is depend on django settings.NINJA["DATETIME_FORMAT"]. or NinjaAPI() Object
# ninja/schema.py
# this is Ninja's Object
class Schema(BaseModel):
class Config:
json_encoders = {
datetime: something config for datetimeformat~~~,
date: ...
time: ....
timedelta : ....
}
it's just proposal , ninja inspired by fastapi , so I don't think this architecture which fully depends on pydantic is bad either. thanks
Per instance It's tricky...
imagine you have two NinjaAPI instances and one schema
in one instance you may want one datetime format, and in other different... and once pydantic classes are created - you cannot reuse pydantic mechanics anymore and will have to manipulate raw data..
so only Global (settings.py) seems possible - but then I would want one schema to have one dateformat and other schema other... so here again - defining some base class in project would make more sense
I understand your thoughts , thanks
I think it would be good make override pydantic section for datetime format in the documentation section
can I pull request these contents?
maybe it's looks appropriate Overriding Pydantic Config Section (https://django-ninja.rest-framework.com/tutorial/config-pydantic/)
Overriding Pydantic Config
...
Example Camel Case mode
...
Example Custom datetime format. <-- new section
...
can I pull request these contents?
sure - never hesitate ;)
What's the status of this proposal? @vitalik @KimSoungRyoul