django-ninja icon indicating copy to clipboard operation
django-ninja copied to clipboard

[proposal] need DATETIME_FORMAT to ninja Schema

Open KimSoungRyoul opened this issue 4 years ago • 8 comments

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 avatar Oct 13 '21 03:10 KimSoungRyoul

@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.

stephenrauch avatar Oct 13 '21 03:10 stephenrauch

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

vitalik avatar Oct 13 '21 07:10 vitalik

but yeah.. this question pops up pretty often... maybe it worth at least some documentation section

vitalik avatar Oct 13 '21 07:10 vitalik

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

KimSoungRyoul avatar Oct 13 '21 07:10 KimSoungRyoul

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

vitalik avatar Oct 13 '21 07:10 vitalik

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

    ...


KimSoungRyoul avatar Oct 13 '21 08:10 KimSoungRyoul

can I pull request these contents?

sure - never hesitate ;)

vitalik avatar Oct 13 '21 08:10 vitalik

What's the status of this proposal? @vitalik @KimSoungRyoul

wu-clan avatar Jan 06 '22 04:01 wu-clan