this post was submitted on 04 Jul 2025
12 points (100.0% liked)

Python

7271 readers
1 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

๐Ÿ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

๐Ÿ Python project:
๐Ÿ’“ Python Community:
โœจ Python Ecosystem:
๐ŸŒŒ Fediverse
Communities
Projects
Feeds

founded 2 years ago
MODERATORS
top 6 comments
sorted by: hot top controversial new old
[โ€“] [email protected] 1 points 1 day ago* (last edited 1 day ago)

Oh btw there are three choices, not two.

  1. stdlib dataclasses

  2. pydantic dataclasses

  3. SQLModel mixin

How to use Mixins

[โ€“] [email protected] 2 points 1 day ago* (last edited 1 day ago)

pydantic underneath (pydantic-base) is written in Rust. fastapi is fast cuz of pydantic. fastapi is extremely popular. Cuz it's right there in the word, fast. No matter how crap the usability is, the word fast will always win.

If fastapi is fast then whatever is not fastapi is slow. And there is no convincing anyone otherwise so lets not even try.

Therefore lets do fast. Cuz we already agreed slow would be bad.

normal dataclasses is not fast and therefore it's bad. If it had a better marketing team this would be a different conversation.

SQLModel combines pydantic and SQLAlchemy.

At first i feel in love with the SQLModel docs. Then realized the eye wateringly beautiful docs are missing vital details, such as how to:

  1. create a Base without also creating a Model table

  2. overload __tablename__ algo from the awful default cls.__name__.lower()

  3. support multiple databases each containing the same named table

#2 is particularly nasty. SQLModel.__new__ implementation consists of multiple metaclasses. So subclasses always inherit that worthless __tablename__ implementation. And SQLAlchemy applies three decorators, so figuring out the right witchcraft to create the Descriptor is near impossible. pydantic doesn't support overriding __tablename__

Then i came along

After days of, lets be honest, hair loss and bouts of heavy drinking, posted the answer here.

Required familiarity with pydantic, sqlalchemy, and SQLModel.

[โ€“] [email protected] 11 points 3 days ago (1 children)

Depends on what you are trying to accomplish, pydantic is great for encoding/decoding

If you don't need that go with dataclasses

[โ€“] [email protected] 3 points 3 days ago

I was going to comment this too

[โ€“] [email protected] 8 points 3 days ago

Keep it simple, stupid.

I'd use the built-in dataclass (or something even simpler) where possible. Only when I need more functionalities (e.g. validation) would I switch to using Pydantic model.

[โ€“] [email protected] 4 points 3 days ago

For validation and/or (de)serialization: pydantic.

Otherwise, dataclasses.

I also like to replace tuples with NamedTuple, but I tend to use tuples a lot in the first place. Itโ€™s generally recommended to use dataclasses instead if you donโ€™t specifically need tuple-like behavior.