Comments (8)
@ID2343242 try replacing the Decimal
field annotation with beanie.DecimalAnnotation
.
from beanie.
I have just tried using find on this same Document and it throws the same validation error. So currently I can only get insert to work with a Document containing a Decimal field.
from beanie.
Hi! Thank you for the catch. I'll check and fix it this/next week.
from beanie.
@ID2343242 try replacing the
Decimal
field annotation withbeanie.DecimalAnnotation
.
thank brocha
from beanie.
Hi @ID2343242 ,
Could you please try beanie.DecimalAnnotation
?
from beanie.
I came up with this solution using a decorator:
from typing import Any, Callable, TypeVar, cast
import bson
from pydantic import BaseModel, create_model, model_validator
T = TypeVar("T", bound="BaseModel")
def beanie_decimal(*fields: str) -> Callable[[type[T]], type[T]]:
def validator(cls: type[T], v: Any) -> Any:
for f in (f.split(".") for f in fields):
current = v
while (field := f.pop(0)) and current:
if not len(f):
if value := current.get(field, None):
current[field] = (
value.to_decimal()
if isinstance(value, bson.Decimal128)
else value
)
break
current = current.get(field, None)
return v
def decorator(model: type[T]) -> type[T]:
return create_model(
model.__name__,
__base__=model,
__module__=model.__module__,
__validators__={
"beanie_decimal": cast(
classmethod, model_validator(mode="before")(validator)
)
},
)
return decorator
@beanie_decimal("price")
class Product(Document):
name: str
price: Decimal = Field(decimal_places=3)
product = Product(name="Apple", price=Decimal('1.99'))
await product.save()
The decorator supports multiple fields being passed in and allows you to define nested model paths.
from beanie.
This issue is stale because it has been open 30 days with no activity.
from beanie.
This issue was closed because it has been stalled for 14 days with no activity.
from beanie.
Related Issues (20)
- [BUG]before_event bug HOT 4
- [BUG] Missing documentation for `beanie.odm.BulkWriter` HOT 1
- [BUG] (some) type annotations for session wrong HOT 3
- [BUG] type annotation for init_beanie() should use Sequence
- [BUG] before_event of type Update HOT 5
- [BUG] mypy does not raise error when trying to access a non-existing attribute from a beanie Document. HOT 1
- [BUG] Indexed unique will not work after Field regex. HOT 2
- [BUG] Empty BackLink for `Optional[BackLink[ADoc]]` when document has no back-link HOT 3
- Concerns and Suggestions Regarding Beanie Library HOT 2
- [Doc] Document for soft delete feature HOT 1
- [BUG] Order of elements in list[Link] is not preserved HOT 2
- [BUG] pydantic computed properties omitted during `insert_many` operation on Document HOT 1
- [BUG] get_motor_collection() returning None under volume only HOT 1
- [BUG] search criteria not using pydantic Field alias to set the key.
- [BUG] `PydanticObjectId` isn't properly configured when deserializing from JSON
- depth search "find(Teste.item1.item2.item3.id == id)" [BUG]
- [BUG] Link not working in find queries on fields with alias
- [BUG]
- [BUG] BulkWriter does not allow unordered bulks
- [BUG] `original_field` is not supported by pydantic, makes typecheckers give a type error and causes a warning at runtime
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 beanie.