Is your feature request related to a problem? Please describe.
In my scenario, I need to generate models that may have multiple inheritance between each other.
customBasePath nicely takes care of the import and inheritance for a single base class.
Describe the solution you'd like
There are multiple ways to achieve this, in order of (personal) preference:
- allow
customTypePath under allOf. This would enable code reuse, as it is already treated as an alternative to type. It might also increase confusion on when to use customTypePath vs customBasePath, as they (now) serve a similar purpose.
- make
customBasePath also accept List[str] instead of just str. This will need backwards compatibility and also the name is not suggestive (singular).
- introduce a new custom keyword
customBasePaths, the list counterpart of customBasePath. Makes it confusing when both are specified and/or together with allOf.
Describe alternatives you've considered
Currently, I work around this using custom_template_dir and additional_imports.
# src/generate.py
from collections import defaultdict
from pathlib import Path
import datamodel_code_generator
additional_imports = ['mymodule.mybaseclass1.MyBaseClass1', 'mymodule.mybaseclass2.MyBaseClass2']
extra_template_data = defaultdict(dict)
extra_template_data['MySubClass'] = [additional_import.split('.')[-1] for additional_import in additional_imports]
datamodel_code_generator.generate(...,
custom_template_dir=Path(__file__).parent,
extra_template_data=extra_template_data,
additional_imports=additional_imports,
)
{# src/pydantic_v2/BaseModel.jinja2 #}
{% set bases = [] %}
{% if base_classes %}{% set bases = bases + base_classes %}{% endif %}
{% if extra_base_classes %}{% set bases = bases + extra_base_classes %}{% endif %}
{% if bases|length == 0 or base_class != "BaseModel" %}{% set bases = bases + [base_class] %}{% endif %}
{# if this is just going to be `class Foo(Bar): pass`, then might as well just make Foo
an alias for Bar: every pydantic model class consumes considerable memory. #}
{% if bases|length == 1 and not fields and not config %}
{{ class_name }} = {{ bases[0] }}
{% else -%}
{% for decorator in decorators -%}
{{ decorator }}
{% endfor -%}
class {{ class_name }}({{ bases|join(', ') }}):{% if comment is defined %} # {{ comment }}{% endif %}
...
Is your feature request related to a problem? Please describe.
In my scenario, I need to generate models that may have multiple inheritance between each other.
customBasePathnicely takes care of the import and inheritance for a single base class.Describe the solution you'd like
There are multiple ways to achieve this, in order of (personal) preference:
customTypePathunderallOf. This would enable code reuse, as it is already treated as an alternative totype. It might also increase confusion on when to usecustomTypePathvscustomBasePath, as they (now) serve a similar purpose.customBasePathalso acceptList[str]instead of juststr. This will need backwards compatibility and also the name is not suggestive (singular).customBasePaths, the list counterpart ofcustomBasePath. Makes it confusing when both are specified and/or together withallOf.Describe alternatives you've considered
Currently, I work around this using
custom_template_dirandadditional_imports.