georgebv / drf-pydantic Goto Github PK
View Code? Open in Web Editor NEWUse pydantic with the Django REST framework
License: MIT License
Use pydantic with the Django REST framework
License: MIT License
Hello, i have encountered some strange behavior while trying to use .drf_serializer() method. I have BaseModel what looks like this
class ExampleDTO(BaseModel):
field_1: Optional[Annotated[int, Field(description="Field1")]] = None
field_2: Optional[Annotated[int, Field(description="Field2")]] = None
field_3: Optional[Annotated[str, Field(description="Field3")]] = None
and this return me next error message:
drf_pydantic.errors.ModelConversionError: Error when converting model: ExampleDTO
field_1
int is not a supported composite type.
field_2
int is not a supported composite type.
field_3
str is not a supported composite type.
Can you please suggest some solution?
Repro:
from drf_pydantic import BaseModel
from pydantic import model_validator
class A(BaseModel):
@model_validator(mode='after')
def qwe(self):
assert False
A.drf_serializer(data={}).is_valid(raise_exception=True) # Nothing happens :(
Probably TextChoises could be treated the same way as Enums. Now it raises an assertion error.
Would be nice to have it. I tried to convert it myself, stuck on types.GenericAlias
and AttributeError: '_SpecialForm' object has no attribute 'origin'
This project seems to have a similar goal to drf-pydantic
:
Perhaps the two projects could work together so that the community can use a single package with the best of both projects?
Apparently, there is a problem with using union types with drf-pydantic.
The following model ...
class DragenStyleMetric(drf_pydantic.BaseModel):
"""Pydantic model for Dragen-style quality control metric entries"""
#: The section of the value
section: str | None
#: The "entry" in the section can be empty or the read group / sample
entry: str | None
#: The name of the metric
name: str | None
#: The count / ratio / time / etc. value
value: int | float | str | None
#: The count as percentage / seconds
value_float: float | None = None
... leads to the following error:
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run
autoreload.raise_last_exception()
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/django/core/management/__init__.py", line 375, in execute
autoreload.check_errors(django.setup)()
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/django/apps/config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/home/holtgrem_c/Development/varfish-server/cases_import/models.py", line 32, in <module>
from cases_qc.io import cramino as io_cramino
File "/home/holtgrem_c/Development/varfish-server/cases_qc/io/cramino.py", line 7, in <module>
from cases_qc import models
File "/home/holtgrem_c/Development/varfish-server/cases_qc/models.py", line 16, in <module>
class DragenStyleMetric(drf_pydantic.BaseModel):
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/drf_pydantic/base_model.py", line 13, in __new__
setattr(cls, "drf_serializer", create_serializer_from_model(cls))
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/drf_pydantic/parse.py", line 68, in create_serializer_from_model
fields[field_name] = _convert_field(field)
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/drf_pydantic/parse.py", line 141, in _convert_field
if field.type_.__origin__ is typing.Literal:
AttributeError: 'types.UnionType' object has no attribute '__origin__'
Having a field such as:
reasons_for_joining: Optional[List[str]] = None
results in the following serializer field:
ListField(child=CharField(allow_null=True, default=None, required=False))
This does not match the field type and providing None to the field results in an error saying reasons_for_joining
cannot be null.
I'm getting the following error when I have schemas with List fields:
from typing import List
from drf_pydantic import BaseModel
class Item(BaseModel):
type: str
quantity: int
is_new: bool
class CartSchema(BaseModel):
items: List[Item]
CartSchema.drf_serializer
This raise the AssertionError at:
drf-pydantic/src/drf_pydantic/parse.py
Line 128 in 01010e2
Having a model with a Decimal field is converted to a DecimalField
in the serializer but a DecimalField
requires max_digits
and decimal_places
to be provided, which is currently not supported.
You can provide max_digits
and decimal_places
to the field:
amount: Decimal = Field(max_digits=10, decimal_places=2)
but these values do not get propagated to the serializer field.
Also of note is that the above method has a type of ConstrainedDecimal
which does not exist in the FIELD_MAP
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.