Git Product home page Git Product logo

Comments (8)

gsakkis avatar gsakkis commented on June 14, 2024 1

@ID2343242 try replacing the Decimal field annotation with beanie.DecimalAnnotation.

from beanie.

ID2343242 avatar ID2343242 commented on June 14, 2024

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.

roman-right avatar roman-right commented on June 14, 2024

Hi! Thank you for the catch. I'll check and fix it this/next week.

from beanie.

maxktz avatar maxktz commented on June 14, 2024

@ID2343242 try replacing the Decimal field annotation with beanie.DecimalAnnotation.

thank brocha

from beanie.

roman-right avatar roman-right commented on June 14, 2024

Hi @ID2343242 ,
Could you please try beanie.DecimalAnnotation ?

from beanie.

ogtega avatar ogtega commented on June 14, 2024

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.

github-actions avatar github-actions commented on June 14, 2024

This issue is stale because it has been open 30 days with no activity.

from beanie.

github-actions avatar github-actions commented on June 14, 2024

This issue was closed because it has been stalled for 14 days with no activity.

from beanie.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.