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

Django custom fields and datetime.time format

Open aambrozkiewicz opened this issue 3 years ago • 1 comments

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)

aambrozkiewicz avatar Jan 29 '22 19:01 aambrozkiewicz

Hi @aambrozkiewicz

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

vitalik avatar Jan 30 '22 13:01 vitalik