-
|
I have dozens of dataclass classes, and I'd like to set |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hi @bramp! Yes — you can achieve this by defining a shared base class with the desired Meta configuration, and then subclassing it for each of your dataclasses. This lets you centralize the configuration without repeating it on every class. Here’s a simple example using the from dataclasses import dataclass
from datetime import datetime
from uuid import UUID
from pprint import pp
from dataclass_wizard import JSONWizard, LoadMeta, fromdict
class BaseConfig(JSONWizard):
class _(JSONWizard.Meta):
v1 = True
v1_on_unknown_key = 'RAISE'
@dataclass
class ProfileModel(BaseConfig):
id: UUID
name: str
created_at: datetime
profile_json_msg = {
"profileId": "123e4567-e89b-12d3-a456-426614174000",
"name": "John Doe",
"createdAt": "2021-01-01T00:00:00Z",
"extra_key": "not mapped to a field!",
}
LoadMeta(
v1=True,
v1_case="AUTO", # older versions used `v1_key_case`
v1_field_to_alias={
"id": "profileId",
},
).bind_to(ProfileModel)
profile = fromdict(ProfileModel, profile_json_msg)
pp(profile)Raises an error when subclassing Let me know if this solves your use case. Thanks! |
Beta Was this translation helpful? Give feedback.
Hi @bramp! Yes — you can achieve this by defining a shared base class with the desired Meta configuration, and then subclassing it for each of your dataclasses. This lets you centralize the configuration without repeating it on every class.
Here’s a simple example using the
v1opt-in: