Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

353
Vistas
How to use DateTimeTZRange in FastAPI with SQLModel (sqlalchemy + pydantic)?

I'm working on a FastAPI project that uses SQLModel as ORM. As far as I understand it SQLModel is some sort of wrapper on top of sqlalchemy and pydantic that makes the two work together. I have a model that looks like this:

from psycopg2.extras import DateTimeTZRange
from pydantic import conlist
from sqlalchemy import Column
from sqlalchemy.dialects.postgresql import TSTZRANGE
from sqlmodel import Field

from app.models.base import BaseModel

class MyModel(BaseModel, table=True):
    # time_range: conlist(datetime, min_items=2, max_items=2) = Field(
    time_range: DateTimeTZRange = Field(
        sa_column=Column(TSTZRANGE()), nullable=False, index=False
    )

    class Config:
        arbitrary_types_allowed = True

When I try to save an instance of this model, I get the following error:

sqlalchemy.exc.DBAPIError: (sqlalchemy.dialects.postgresql.asyncpg.Error) <class 'asyncpg.exceptions.DataError'>: invalid input for query argument $1: DateTimeTZRange(datetime.datetime(2019, ... (list, tuple or Range object expected (got type <class 'psycopg2._range.DateTimeTZRange'>))
[SQL: INSERT INTO my_model (time_range) VALUES (%s) RETURNING my_model.id]
[parameters: (DateTimeTZRange(datetime.datetime(2019, 4, 2, 23, 0, tzinfo=<UTC>), datetime.datetime(2019, 4, 2, 23, 30, tzinfo=<UTC>), '[)'))]

The commented line (using conlist) didn't give any errors, but it always saved null to the database. I couldn't find any resources or documentation online regarding how to make this work. Any ideas what I might be doing wrong?

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda