Comments (3)
@Donokami That's a really nice solution!
What I've been doing in the past is something like:
MyTableBaseModel = create_pydantic_model(MyTable)
class MyTableModel(MyTableBaseModel):
...
But what you've done is definitely cleaner. I'll modify this bit of the docs to include it.
I upgraded VSCode, and Pylance is way more aggressive now. Previously I didn't encounter any warnings with the Piccolo project, but now there are quite a few I need to look into.
from piccolo.
Interesting - I haven't run Pylance in strict mode before. I just turned it on to have a look.
I've seen the Variable not allowed in type expression
error before. It's because create_pydantic_model
creates a new class, which is assigned to a variable. Type checkers don't like it when you use a variable as a type. Functionally it works fine, so it's OK to ignore these errors.
The way we'll fix that long term is having an alternative to create_pydantic_model
which works like this instead:
class MyModel(PiccoloPydanticModel, table=MyTable):
...
As for the Type of "create_pydantic_model" is partially unknown
error, I made this example script:
from piccolo.table import Table
from piccolo.columns import Varchar
from piccolo.utils.pydantic import create_pydantic_model
class MyTable(Table):
name = Varchar()
MyModel = create_pydantic_model(
table=MyTable,
)
def bar(model: MyModel):
pass
The cause might be that Pylance is looking at Pydantic v1 for some reason. It tries to find pydantic.config.ConfigDict
and it's can't because that's only available in Pydantic v2.
![Screenshot 2023-11-03 at 15 42 33](https://private-user-images.githubusercontent.com/350976/280335192-6cf4c860-1e3c-4ae3-95b7-99c04424a5cd.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTY3OTU0OTksIm5iZiI6MTcxNjc5NTE5OSwicGF0aCI6Ii8zNTA5NzYvMjgwMzM1MTkyLTZjZjRjODYwLTFlM2MtNGFlMy05NWI3LTk5YzA0NDI0YTVjZC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNTI3JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDUyN1QwNzMzMTlaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT03NDI1NmFjN2Y4YmVmM2I1YTNhODc1MTY2OWM1MzY5ZDVmYjQ2N2M3NzIwY2U0ZTFkYjdkZjlhOGQ3NWYyM2NmJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.-XNCnIrb7UjZ7gGXavbXnhRuLMeIU1Fr64dnBlm7QnA)
Have a quick look to see if that's the case.
from piccolo.
Hey!
I've been trying things for a while, and I found something really quite cool, that works with basic mode.
This works with Pylance:
class Image_pydantic(
create_pydantic_model(
table=Image,
exclude_columns=(Image.parameters, Image.author),
)
):
pass
This syntax, for declaring a Pydantic model, is really interesting because it also allows us to change how we serialize specific fields.
For example, I can do this:
class Expense_pydantic(
create_pydantic_model(
table=Expense,
exclude_columns=(Expense.customer,),
nested=True,
)
):
image: Image_pydantic
And it will automatically use the above pydantic model to serialize nested data from Expense.
Another example of interesting use of this declaration:
class Customer_pydantic(
create_pydantic_model(
table=Customer,
include_columns=(
Customer.id,
Customer.email,
Customer.created_on,
Customer.updated_on,
Customer.external_token,
),
)
):
external_token: Annotated[
Optional[str],
Field(
max_length=40,
examples=["r8_FoO**********************************"],
),
] = None
@model_validator(mode="after")
def post_root(self) -> "Customer_schema":
if self.external_token is not None and len(self.external_token) > 7:
self.external_token = self.external_token[:6] + "*" * (
len(self.external_token) - 6
)
if self.external_token == "":
self.external_token = None
return self
This would share a field stored in the DB, but only give a hint about its content.
For the Pylance error on create_pydantic_model
, the only one I see is this one:
Thanks for the fast answer and for your work btw, Piccolo is trully underrated!!
from piccolo.
Related Issues (20)
- enums not reflect in object response HOT 2
- Using `db_column_name` when adding columns to existing tables using auto migrations HOT 1
- Adding a self referencing foreign key to an existing table which has a custom primary key
- `ModelBuilder` and recursive foreign keys
- Add `array_columns` to `Table._meta`
- Add a method to the `Array` column for getting the number of dimensions of the array
- Make it clearer how multiple `where` clauses work in the docs
- Remove banner from docs saying about Piccolo v1
- Bump black version
- Add a utility method for getting the innermost type of an `Array` column
- Add support for collation HOT 1
- Add a `having` clause
- `Serial` field won't work in SQLite if it is not the primary key
- `Serial` field won't updates automatically aftes saving in Postgres
- Are bit types supported? HOT 2
- Add one more Engine for PostgreSQL HOT 7
- Support for callbacks at Table level for create update delete HOT 7
- Auto-created index name have a big chance to clash with the table name or name of some structure in the database. HOT 1
- Pressing Enter in `Reverse 1 migration? [y/N]` still leads to reversing. HOT 2
- Add support for `upper` and `lower`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from piccolo.