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

"none is not an allowed value" #159

Closed
2ik opened this issue Jun 21, 2021 · 7 comments
Closed

"none is not an allowed value" #159

2ik opened this issue Jun 21, 2021 · 7 comments

Comments

@2ik
Copy link

2ik commented Jun 21, 2021

I found an error on the part of pydantic
Example:

# model.py
TestModel(models.Model):
    revenue_total = models.FloatField(default=0, null=True, blank=True)

# schemas.py
TestSchema = create_schema(TestModel)

# api.py
@api.get("/tests", response=List[TestSchema])
def get_tests(request):
    return TestModel.objects.all()

output:

Traceback (most recent call last):
  File "venv/lib/python3.8/site-packages/ninja/operation.py", line 94, in run
    return self._result_to_response(request, result)
  File "venv/lib/python3.8/site-packages/ninja/operation.py", line 177, in _result_to_response
    result = response_model.from_orm(resp_object).dict(
  File "pydantic/main.py", line 631, in pydantic.main.BaseModel.from_orm
pydantic.error_wrappers.ValidationError: 1 validation error for NinjaResponseSchema
response -> 5 -> revenue_total
  none is not an allowed value (type=type_error.none.not_allowed)

exception if the value in db is NULL

@2ik
Copy link
Author

2ik commented Jun 21, 2021

temporary solution:

class TestSchema(Schema):
    revenue_total: float = None

@vitalik
Copy link
Owner

vitalik commented Jun 22, 2021

@2ik
Hi it's not clear where/how you defined field revenue_total ? are you sure it's present in your model and null=True ?

@2ik
Copy link
Author

2ik commented Jun 24, 2021

Hi, sorry, this is a typo, I just used it for an example. Changed in the question.
in db:
image

The error happens only when null is in the record. This seems to be due to the fact that there is a default value in model

@vitalik
Copy link
Owner

vitalik commented Jul 8, 2021

well I cannot reproduce this..

maybe you can give more details.. your actual model and schema
and also ninja version

@2ik
Copy link
Author

2ik commented Jul 8, 2021

Thank you for your response

Have you filled in the "revenue_total" field with value "None"? Not in admin, try directly in the database for example.

v0.13.2

@vitalik
Copy link
Owner

vitalik commented Jul 8, 2021

ok, I know whats' your issue

you defined both default=0, null=True , which I cannot find any logical sense...

django ninja basically uses the default first to create schema


for you solution would be

  • either remove default=0
  • either remove null=True - but then patch all current null values with 0

PS. btw counting money with float field is not a good idea (if total revenue is money) - you should consider decimal (or integer coins) instead

@2ik
Copy link
Author

2ik commented Jul 8, 2021

ok, understand. I just thought it was a bug in the package)) Yes, I have bad logic with the model, but I needed "default" for admin panel only. I will make changes to in method "save"

As for the float field, it suits me better, because the numbers are approximate. I know about the money field, but thank you for paying attention to this ;)

@vitalik vitalik closed this as completed Jul 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants