Skip to content

Create Schema in resolve function #1403

Closed Answered by RemiDesgrange
RemiDesgrange asked this question in Q&A
Discussion options

You must be logged in to vote

I'm replying with the answer.

As I'm re-reading my question, I forgot the OrderOut Schema, which was the culprit here.

here is the version I had:

class OrderOut(ModelSchema):
    class Meta:
         model = Order
        fields = ("reference", ...)
    product: ProductOut

    @staticmethod
    def resolve_product(order: Order) -> ProductOut:
          return ProductOut.model_validate(order.order_product.product)

After reading stack traces and running the debugger I found the problem, Ninja ModelSchema will take care of the model_validate for me.
So, 2 versions that worked:

class OrderOut(ModelSchema):
    class Meta:
         model = Order
        fields = ("reference", ...)
    product: 

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by RemiDesgrange
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant