django2pydantic
Django2pydantic is the most complete library for converting Django ORM models to Pydantic models
Science Score: 44.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (11.5%) to scientific vocabulary
Keywords
Repository
Django2pydantic is the most complete library for converting Django ORM models to Pydantic models
Basic Info
Statistics
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 13
- Releases: 21
Topics
Metadata Files
README.md
Why
django2pydantic is the most complete Pydantic schemas based on Django models.
What
django2pydantic is a library that allows to define Pydantic schemas based on Django database models.
Similar libraries:
Key features
- Supports all Django model field types
- Supports @property decorated Django model methods
- Supports all Django model relation fields:
- ForeignKey, OneToOneField, ManyToManyField
- The reverse relations of the above (ManyToOneRel, OneToOneRel, ManyToManyRel)
- Supports defining nested relations
- Provides as complete OpenAPI schema details as possible
- Support for SchemaField
How to use
See the following usage examples:
- Basic usage example
- Overriding Django field properties by using
InferExcept - Making required Django fields optional in Pydantic schema
Some details
Django fields blank and null
There are some nuances related to the use of blank and null in Django fields
and how they impact the output pydantic types, but generally:
- If null=True the output type will be Optional (i.e. str | None).
- If blank=True the field will be optional (with exceptions).
This is to be more in line with the Django admin/form validation behavior.
- Whether the field is required is determined by the default value (can be overridden).
- If the default is PydanticUndefined, the field is always required.
- If the default is None (or "" in case of string), the field is optional.
- If null=False and blank=True, the field typically requires implementing clean()
on the model in order to programmatically supply any missing values. In our case, the
required/default value can vary depending on field type in a way that user
does not need to concern with implementing clean().
String-based fields
This covers the following Django field types: - CharField - SlugField - EmailField - URLField - TextField
| Configuration | Required | Default
(If not specified) | Type | Min length | Comments |
|---------------------------|--------------|-------------------------------|---------------|------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| null=True, blank=True | Optional | None | str \| None | None | There's 2 ways to represent "no data" in the DB: null and "" (not recommended) |
| null=True, blank=False | Required | PydanticUndefined | str \| None | 1 | • There's 2 ways to represent "no data" in the DB: null and "" (not recommended)
• However, with min_length, we eliminate "" from being a valid input |
| null=False, blank=True | Optional | "" | str | None | Default is "" since null is not allowed |
| null=False, blank=False | Required | PydanticUndefined | str | 1 | |
Non-string-based fields
This covers the following Django field types: - IntegerField - SmallIntegerField - IntegerField - BigIntegerField - PositiveSmallIntegerField - PositiveIntegerField - PositiveBigIntegerField - FloatField - DecimalField - BinaryField - BooleanField - DateField - DateTimeField - DurationField - FileField - FilePathField - GenericIPAddressField - ImageField - JSONField - TimeField - UUIDField
[FieldType] varies depending on the Django field type
| Configuration | Required | Default
(If not specified) | Type | Comments |
|---------------------------|--------------|-------------------------------|-----------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------|
| null=True, blank=True | Optional | None | [FieldType] \| None | |
| null=True, blank=False | Required | PydanticUndefined | [FieldType] \| None | |
| null=False, blank=True | Required | PydanticUndefined | [FieldType] | Unlike string-based fields, without a specified default value, there's no valid empty value to be stored in database thus making this required |
| null=False, blank=False | Required | PydanticUndefined | [FieldType] | |
Relational fields
This covers the following Django field types: - ForeignKey - OneToOneField - ManyToManyField - ManyToOneRel - OneToOneRel - ManyToManyRel
[PkType] varies depending on model's primary key type
ForeignKey, OneToOneField
| Configuration | Required | Default
(If not specified) | Type | Comments |
|---------------------------|--------------|-------------------------------|--------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------|
| null=True, blank=True | Optional | None | [PkType] \| None | |
| null=True, blank=False | Required | PydanticUndefined | [PkType] \| None | |
| null=False, blank=True | Required | PydanticUndefined | [PkType] | Unlike string-based fields, without a specified default value, there's no valid empty value to be stored in database thus making this required |
| null=False, blank=False | Required | PydanticUndefined | [PkType] | |
ManyToManyField
null has no effect since there is no way to require a relationship at the database level.
Ref: https://docs.djangoproject.com/en/5.1/ref/models/fields/#manytomanyfield
| Configuration | Required | Default
(If not specified) | Type | Comments |
|---------------|--------------|-------------------------------|------------------------|----------|
| blank=True | Optional | None | list[PkType] \| None | |
| blank=False | Required | PydanticUndefined | list[PkType] \| None | |
OneToOneRel
This field is created as a result of OneToOneField and is not directly configurable.
| Required | Default
(If not specified) | Type | Comments |
|----------|-------------------------------|--------------------|----------|
| Optional | None | [PkType] \| None | |
| Optional | None | [PkType] \| None | |
| Optional | None | [PkType] \| None | |
| Optional | None | [PkType] \| None | |
ManyToOneRel, ManyToManyRel
These fields are created as a result of ForeignKey, ManyToManyField and are not directly configurable.
| Required | Default
(If not specified) | Type | Comments |
|----------|-------------------------------|------------------------|----------|
| Optional | None | list[PkType] \| None | |
| Optional | None | list[PkType] \| None | |
| Optional | None | list[PkType] \| None | |
| Optional | None | list[PkType] \| None | |
Owner
- Name: NextGenContributions
- Login: NextGenContributions
- Kind: organization
- Repositories: 1
- Profile: https://github.com/NextGenContributions
Citation (CITATION.cff)
# This CITATION.cff file was generated with cffinit.
# Visit https://bit.ly/cffinit to generate yours today!
cff-version: 1.2.0
title: django2pydantic
message: >-
If you use this software, please cite it using the
metadata from this file.
type: software
authors:
- given-names: Jukka
family-names: Hassinen
email: jukka.hassinen@gmail.com
repository-code: 'https://github.com/NextGenContributions/django2pydantic'
abstract: Convert Django models to Pydantic models/schema
keywords:
- Django
- Pydantic
- model
- schema
license: MIT
Issues and Pull Requests
Last synced: 4 months ago
All Time
- Total issues: 9
- Total pull requests: 129
- Average time to close issues: 12 days
- Average time to close pull requests: 3 days
- Total issue authors: 2
- Total pull request authors: 6
- Average comments per issue: 0.0
- Average comments per pull request: 5.12
- Merged pull requests: 75
- Bot issues: 0
- Bot pull requests: 94
Past Year
- Issues: 9
- Pull requests: 129
- Average time to close issues: 12 days
- Average time to close pull requests: 3 days
- Issue authors: 2
- Pull request authors: 6
- Average comments per issue: 0.0
- Average comments per pull request: 5.12
- Merged pull requests: 75
- Bot issues: 0
- Bot pull requests: 94
Top Authors
Issue Authors
- phuongfi91 (5)
- jhassine (4)
Pull Request Authors
- github-actions[bot] (48)
- dependabot[bot] (37)
- phuongfi91 (28)
- jhassine (7)
- renovate[bot] (7)
- deepsource-autofix[bot] (2)