Giter Club home page Giter Club logo

drf-pydantic's Introduction

drf-pydantic's People

Contributors

djbios avatar georgebv avatar mrmika96 avatar shkangomelet avatar thommor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

drf-pydantic's Issues

<type> is not a supported composite type.

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?

Support python 3.7

Would be nice to have it. I tried to convert it myself, stuck on types.GenericAlias and AttributeError: '_SpecialForm' object has no attribute 'origin'

AttributeError: 'types.UnionType' object has no attribute '__origin__'

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__'

Optional lists are no supported

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.

AssertionError for list fields

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:

assert isinstance(field.outer_type_, types.GenericAlias)

Can't use DecimalFields

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.