Replies: 1 comment 4 replies
-
So the situation is something like this? class Band(Table):
name = Varchar()
popularity = Integer()
@property
def is_top_band(self):
return self.popularity > 1000 If you're using Here's a quick experiment I put together: from piccolo.table import Table
from piccolo.columns import Varchar, Integer
from piccolo.utils.pydantic import create_pydantic_model
from piccolo.engine.sqlite import SQLiteEngine
DB = SQLiteEngine()
class Band(Table, db=DB):
name = Varchar()
popularity = Integer()
@property
def is_top_band(self):
return self.popularity > 1000
BandModel = create_pydantic_model(Band)
class CustomBandModel(BandModel):
is_top_band: bool
class Config:
orm_mode = True
if __name__ == '__main__':
Band.create_table(if_not_exists=True).run_sync()
band = Band.objects().get_or_create(Band.name == 'Pythonistas', defaults={Band.popularity: 2000}).run_sync()
model = CustomBandModel.from_orm(band)
print(model.json())
# >>> {"name": "Pythonistas", "popularity": 2000, "is_top_band": true} This might work for your own custom endpoints (if using FastAPI or something). But in terms of PiccoloCRUD(table=Band, properties=[Band.popularity]) And then those properties would be returned by GET requests. Was that what you had in mind? |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've been needing to add calculated properties to fields on output types for different tables. At the moment it looks like there is no way to take property on a table class definition and reflect it in the pydantic type and use it in the responses.
Beta Was this translation helpful? Give feedback.
All reactions