You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an ORM query that creates annotations. While hour makes sense within the context of the query, as a property in the API response, I'd like to call the field time. In the WaterLevelSchema, I can specify hour: datetime, which works but if I only specify time: datetime = Field(..., alias='hour') I get an error:
Field required [type=missing, input_value=<DjangoGetter: WaterLevel...o.ZoneInfo(key='UTC')))>, input_type=DjangoGetter]
For further information visit https://errors.pydantic.dev/2.8/v/missing
How can I create an alias for a field that was created via annotation in a Django ORM query? When I specify both hour and time in the WaterLevelSchema, there is no error, but then I have both properties in the API response, which I do not want. Do I have my thinking backwards? How can this be done?
classWaterLevelSchema(Schema):
hour: datetime# --> workstime: datetime=Field(..., alias='hour') # --> raises an error unless hour is specified as aboveclassDashboardSchema(Schema):
...
historic_water_levels_m3: list[WaterLevelSchema]
@router.get('/dashboard/{location_id}', response=DashboardSchema)defdashboard(request, location_id):
latest_water_levels= (
ExecutionLog.objects
.filter(forecast_id__in=latest_forecasts_ids)
.annotate(day=TruncDay('created_at'), hour=TruncHour('created_at')) # Truncate timestamp to day and hour
.annotate(avg_water_level_m3=Avg('water_level_m3')) # Calculate average water level per hour per day
.values('day', 'hour') # Group by day and hour
)
returnDashboardSchema(
...
historic_water_levels_m3=latest_water_levels
)
The text was updated successfully, but these errors were encountered:
It would seem to me to be pretty normal but I may be mistaken.
You're sending hour attribute to your WaterLevelSchema when it's expecting time.
The syntax for alias is good, but in this case you don't want an alias, you just want a different schema.
So you remove hour from WaterLevelSchema, you define time as time: datetime (not an alias), and you annotate your object to output time instead of hour.
Btw I don't think you need to return DashboardSchema, as it's defined in response=DashboardSchema, that's the whole point, you can return the object as-is, as long as it's in the format you defined.
classWaterLevelSchema(Schema):
time: datetime# (...)@router.get('/dashboard/{location_id}', response=DashboardSchema)defdashboard(request, location_id):
latest_water_levels= (
# (...)
.annotate(day=TruncDay('created_at'), time=TruncHour('created_at')) # Truncate timestamp to day and hour# (...)
)
return {
# (...)historic_water_levels_m3=latest_water_levels
}
I have an ORM query that creates annotations. While
hour
makes sense within the context of the query, as a property in the API response, I'd like to call the fieldtime
. In theWaterLevelSchema
, I can specifyhour: datetime
, which works but if I only specifytime: datetime = Field(..., alias='hour')
I get an error:How can I create an alias for a field that was created via annotation in a Django ORM query? When I specify both
hour
andtime
in theWaterLevelSchema
, there is no error, but then I have both properties in the API response, which I do not want. Do I have my thinking backwards? How can this be done?The text was updated successfully, but these errors were encountered: