vitalik / django-ninja

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

Django custom fields and datetime.time format #343

Open aambrozkiewicz opened 2 years ago

aambrozkiewicz commented 2 years ago

Hey, I got two more questions:

  1. Is there a way to cast some unknown types to Ninja? I noticed that list is limited to what's inside TYPES defined at ninja/orm/fields.py:40. I have a custom field made on top of ArrayField (Postgres) which is essentially a comma delimited string.
  2. I have a TimeField defined on a model and would like to change the format, to drop seconds from it. How this could be done?

One more

I'm using Hashids for my ID, and this snippets works properly, but I'm not sure I'm doing it right because validator doesn't make sense for me when I want to present a value. Is there a better way to do it?

class OrderOut(ModelSchema):
    id: str

    class Config:
        model = models.Order
        model_fields = ("name",)

    @validator("id", pre=True, allow_reuse=True, check_fields=False)
    def hashid(cls, v):
        return v.hashid

I'm using hashid as a primary key field: id = HashidAutoField(primary_key=True)

vitalik commented 2 years ago

Hi @aambrozkiewicz

I guess you can define your own pydantic field types see Custom Data Types