[QUESTION] How to transform data
See original GitHub issueFirst check
- I used the GitHub search to find a similar issue and didn’t find it.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I searched the FastAPI documentation, with the integrated search.
Description
How can I transform data received from ORM when passing to Pydantic?
For example:
We have a table in PostgresQL named objects
, which has 4 columns: id
, title
, lat
, lng
and we created a model to it named ObjectModel
:
class ObjectModel(Base):
__tablename__ = 'objects'
id = Column(Integer, primary_key=True)
title = Column(String)
latitude = Column(Float, name='lat')
longitude = Column(Float, name='lng')
When we make a request (db.query(models.ObjectModel).all()
) to receive all content of the table we will get something like this:
[
{
"id": 1,
"title": "object 1",
"latitude": 12.34,
"longitude": 12.34
},
{
"id": 2,
"title": "object 2",
"latitude": 23.45,
"longitude": 23.45
}
]
How to transform it with Pydantic to something like this:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
"12.34",
"12.34"
]
},
"properties": {
"id": 1,
"title": "object 1"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
"23.45",
"23.45"
]
},
"properties": {
"id": 2,
"title": "object 2"
}
}
]
}
Or how to return objects instead of fields: Instead of
{
"id": 1,
"title": "object 1"
}
return
[
{
"key": "Object number",
"value": 1
},
{
"key": "Object title",
"value": "object 1"
}
]
Additional context
Add any other context or screenshots about the feature request here.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Transforming data problem (article) - Khan Academy
Transforming data problem ... It is very common to take data and apply the same transformation to every data point in the set....
Read more >20 Data Transformation Interview Questions and Answers
Data Transformation Interview Questions and Answers · 1. What is Data Transformation? · 3. What are the main types of data transformations? ·...
Read more >Maths Tutorial: Question on Data Transformations (statistics)
Answering a question sent in about data transformations, and how to transform data to linearity. For more further maths tutes, or to ask...
Read more >Lesson 9: Data Transformations - STAT ONLINE
To introduce basic ideas behind data transformations we first consider a simple linear regression model in which: We transform the predictor (x) values...
Read more >data transformation and recoding
Then you must specify the conditions necessary to change the data. For example, if you wanted to score a question, you would make...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the help here @fullonic ! 🙇 👏
@Andreigr0 as you are not really returning the data as it comes from the DB but applying some transformations, I think that’s extra logic that would probably belong to your specific app. So it would be best to first modify the data to have the shape that you need in your code, and then return it, and have the Pydantic model just for the data with the final shape.
What I mean is, it will be a lot easier to not try and do the transformation of the data in a Pydantic model passed to
response_model
but rather in your own code. It will be a lot easier to debug, understand, etc. And as it is part of your custom business logic, it would probably be better living in your function and not in a Pydantic model.Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.