Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object of type Decimal is not JSON serializable #628

Closed
yubarajshrestha opened this issue Apr 16, 2022 · 1 comment
Closed

Object of type Decimal is not JSON serializable #628

yubarajshrestha opened this issue Apr 16, 2022 · 1 comment
Labels
bug An existing feature is not working as intended

Comments

@yubarajshrestha
Copy link
Contributor

Describe the bug
If you have decimal migrations then you can't return response.

Migration

def up(self):
        """
        Run the migrations.
        """
        with self.schema.create("commerce_products") as table:
            table.increments("id")
            table.string("title")
            table.string("slug").unique()
            table.decimal("price", 19, 2)
            table.timestamps()

Modal

class Product(Model):
    __fillable__ = ["title", "slug", "price"]

Controller

class ProductController(Controller):
    def __init__(self, response: Response, request: Request) -> None:
        self.response = response
        self.request = request

    def index(self):
        """Returns a list of products"""

        per_page = int(self.request.input("per-page", 10))
        page = int(self.request.input("page", 1))

        return  Product.paginate(per_page, page)

Expected behavior
I should see a proper json response

Screenshots or code snippets
Screen Shot 2022-04-16 at 13 40 51

@yubarajshrestha yubarajshrestha added the bug An existing feature is not working as intended label Apr 16, 2022
@josephmancuso
Copy link
Member

josephmancuso commented May 7, 2022

The fix to this is to cast the decimal to a float in the model:

class CommerceProducts(Model):

    __casts__ = {
        "price": "float"
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An existing feature is not working as intended
Projects
None yet
Development

No branches or pull requests

2 participants