You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using fields_optional on a Model which has a field where a default parameter is set causes TypeError: cannot specify both default and default_factory
The full error trace (minus the hot reload stuff) is:
File "C:\Users\CaptainException\ninja_backend\core\urls.py", line 24, in <module>
from rental_service.api import router as rental_router
File "C:\Users\CaptainException\ninja_backend\rental_service\api.py", line 109, in <module>
class RentalPatchSchema(ModelSchema):
File "C:\Users\CaptainException\ninja_backend\.venv\Lib\site-packages\ninja\orm\metaclass.py", line 106, in __new__
model_schema = create_schema(
^^^^^^^^^^^^^^
File "C:\Users\CaptainException\ninja_backend\.venv\Lib\site-packages\ninja\orm\factory.py", line 65, in create_schema
python_type, field_info = get_schema_field(
^^^^^^^^^^^^^^^^^
File "C:\Users\CaptainException\ninja_backend\.venv\Lib\site-packages\ninja\orm\fields.py", line 169, in get_schema_field
FieldInfo(
File "C:\Users\CaptainException\ninja_backend\.venv\Lib\site-packages\pydantic\fields.py", line 210, in __init__
raise TypeError('cannot specify both default and default_factory')
TypeError: cannot specify both default and default_factory
but this defeats the purpose of the __all__ shortcut.
This is a similar bug as #1019 and #1080. The PR #1100 is a fix for this but I don't know if that has any side effects, or if there is a better solution.
When using PATCH on a resource the id is always the only required field, so maybe we could have a fields_optional_exclude option similar to exclude so that we don't have to manually specify all fields, which can be tedious on big Models. An exclude option is shorter and more readable.
But this obviously does not fix the issue for people who are using fields with a default value that can be optional in their Schema.
The text was updated successfully, but these errors were encountered:
CaptainException
changed the title
[BUG] fields_optional ="__all__" causes TypeError on models with a field using the default parameter
[BUG] fields_optional="__all__" causes TypeError on models with a field using the default parameter
Jun 3, 2024
Versions:
Bug Description:
Using
fields_optional
on a Model which has a field where adefault
parameter is set causesTypeError: cannot specify both default and default_factory
The full error trace (minus the hot reload stuff) is:
My Model is defined like this:
And the Schema like this:
Manually specifying all optional fields minus the field using
default
also works:but this defeats the purpose of the
__all__
shortcut.This is a similar bug as #1019 and #1080. The PR #1100 is a fix for this but I don't know if that has any side effects, or if there is a better solution.
When using PATCH on a resource the id is always the only required field, so maybe we could have a
fields_optional_exclude
option similar toexclude
so that we don't have to manually specify all fields, which can be tedious on big Models. An exclude option is shorter and more readable.But this obviously does not fix the issue for people who are using fields with a default value that can be optional in their Schema.
The text was updated successfully, but these errors were encountered: