dymmond / mongoz

ODM with Pydantic made it simple
https://mongoz.dymmond.com
MIT License
13 stars 2 forks source link

Getting issue while storing the decimal value in the document. #42

Open harshalizode opened 4 days ago

harshalizode commented 4 days ago

Hello,

I am trying to store the decimal value in by embedded document:

Class Achived(Document):
    score_from: float = mongoz.Decimal
    score_to: float = mongoz.Decimal(max_digits=5, decimal_places=3)
    amount: float = mongoz.Decimal(
        max_digits=5, decimal_places=3)

When I try to store the decimal value it raise the exception as: InvalidDocument("cannot encode object: Decimal('0.60'), of type: <class 'decimal.Decimal'>") . As I passing the string or float value for it to store in document.

tarsil commented 4 days ago

I will have a look but please give me an example of something you are trying to store to run against

harshalizode commented 4 days ago

I am firing the query as await Achived.objects.create(score_from=0.60, score_to='0.90', amount='78.6786')

But it causing the error if I convert the decimal values in the decimal.Decimal object then it requires the type string, integer or float.

tarsil commented 4 days ago

@harshalizode correct, Pymongo doesn't understand the native Decimal, for some reason they didn't do an internal implementation. So they use a Decimal128, which is now being supported internally by the version 0.10.1.

If you install that one, this issue should be now fixed.

tarsil commented 4 days ago

The docs were also updated with some extra explanations, but the way you do remains the same 👍🏼

tarsil commented 4 days ago

@harshalizode something for the embedded documents was missing from release 0.10.1 for the decimal as well, now it is updated in the 0.10.2, apologies for that.

harshalizode commented 4 days ago

Yes, It has been solved in the latest version. Thank you.