Comments (5)
workaround:
class CustomSerializerDyntasticMixin:
@staticmethod
def serialize(data, model_json_encoders=None):
_serialize = partial(CustomSerializerDyntasticMixin.serialize, model_json_encoders=model_json_encoders)
if isinstance(data, BaseModel):
return _serialize(data.dict())
elif isinstance(data, dict):
return {key: _serialize(value) for key, value in data.items() if value is not None}
elif isinstance(data, (list, tuple)):
return list(map(_serialize, data))
elif isinstance(data, set):
return set(map(_serialize, data))
elif isinstance(data, (Decimal, str, int, bytes, bool, float, type(None))):
return data
else:
if model_json_encoders:
json_encoder = partial(custom_pydantic_encoder, model_json_encoders)
else:
json_encoder = pydantic_encoder
return json.loads(json.dumps(data, default=json_encoder))
def save(self, *, condition: Optional[object] = None):
data = self.dict(by_alias=True)
dynamo_serialized = CustomSerializerDyntasticMixin.serialize(data, model_json_encoders=self.__config__.json_encoders)
return self._dyntastic_call("put_item", Item=dynamo_serialized, ConditionExpression=condition)
class DummyJobClass:
pass
class JobType(Enum):
DUMMY_JOB_TYPE = DummyJobClass
class Job(Dyntastic):
__table_name__ = "jobs"
__hash_key__ = "job_id"
__table_host__ = "http://localhost:8000"
__table_region__ = "us-east-1"
job_id: int
type: JobType
class Config:
json_encoders = {
JobType: lambda obj: obj.name
}
model = Job(job_id=1, type=JobType.DUMMY_JOB_TYPE)
model.save()
I can make a PR if you would like
from dyntastic.
As a different workaround, does something like this work?
from pydantic.json import ENCODERS_BY_TYPE
ENCODERS_BY_TYPE[JobType] = lambda obj: obj.name
from dyntastic.
@nayaverdier I ended up with a custom Enum subclass to handle Enums with an object value because Pydantic v1 has issues with Enums. These issues are fixed in Pydantic v2.
from dyntastic.
Pydantic v2 support is on my roadmap, I'll let you know when it's released.
from dyntastic.
@flolas Dyntastic version 0.13.0
now supports pydantic v2. Closing this ticket but feel free to reopen if you continue to have issues related to this.
from dyntastic.
Related Issues (15)
- Batching HOT 2
- Pydantic v2 HOT 5
- Index documentation / source HOT 2
- Dyntastic import error HOT 1
- support for env variable based metadata HOT 3
- Localstack support HOT 3
- Attr query by class variables. HOT 1
- Consider removing the dependency pinning HOT 4
- Clarifications on item updates HOT 5
- Allowing updates without first fetching the record
- Feature request: __table_host__ or endpoint_url HOT 3
- Transaction methods? HOT 22
- `.query()` over large data sets with a `filter_condition` does not return expected results HOT 4
- [FEATURE] Support KEYS_ONLY secondary index HOT 2
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 dyntastic.