I use the Tortoise-orm model:
from tortoise import fields
from tortoise.models import Model
import uuid
class News(Model):
news_id: str = fields.TextField(pk=True)
title: str = fields.TextField()
link: str = fields.TextField()
img: str = fields.TextField()
pub_date = fields.DatetimeField()
description: str = fields.TextField()
category: str = fields.TextField()
text: str = fields.TextField()
class Meta:
ordering = ['-pub_date']
Here is my endpoint and method for getting data from the database:
@router.get('/news')
async def get_offset_news(start: int, end: int):
try:
print(await newsServices.get_offset_news(start=start, end=end))
return await newsServices.get_offset_news(start=start, end=end)
except Exception as error:
handle_error(error=error)
async def get_offset_news(self, start: int, end: int):
return await News.filter().offset(start).limit(end - start).values()
This is the format in which the date is stored in the database:
2024-05-10 18:58:35+03
Here’s what print returns in the endpoint /news:
‘pub_date’: datetime.datetime(2024, 5, 10, 15, 58, 35, tzinfo=)
And finally, in what form the date comes in the response from the API:
“pub_date”: “2024-05-10T15:58:35+00:00”
I can’t figure out why +03 is changing to +00:00. I think it should be clarified that the database and API are on my home computer.
I have already tried to create a separate model for the response:
class NewsModel(BaseModel):
news_id: str
title: str
link: str
img: str
pub_date: datetime
description: str
category: str
text: str
class Config:
orm_mode = True
json_encoders = {
datetime: lambda dt: dt.strftime('%Y-%m-%d %H:%M:%S%z')
}
But as a result, I got this answer: “pub_date”: “2024-05-10 15:29:08+0000”
I_love_neko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.