{"id":29417017,"url":"https://github.com/simatwa/django-pydantic-models","last_synced_at":"2026-03-04T13:30:51.917Z","repository":{"id":294337737,"uuid":"986636391","full_name":"Simatwa/django-pydantic-models","owner":"Simatwa","description":"A lightweight utility that converts Django models into fully-typed Pydantic models.","archived":false,"fork":false,"pushed_at":"2025-05-20T09:40:49.000Z","size":16,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-27T18:06:02.169Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Simatwa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-05-19T23:00:38.000Z","updated_at":"2025-05-20T09:40:53.000Z","dependencies_parsed_at":"2025-08-12T21:19:29.008Z","dependency_job_id":"805ad08d-81dc-4f1a-9293-4186b27bc83a","html_url":"https://github.com/Simatwa/django-pydantic-models","commit_stats":null,"previous_names":["simatwa/django-pydantic-models"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/Simatwa/django-pydantic-models","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Simatwa%2Fdjango-pydantic-models","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Simatwa%2Fdjango-pydantic-models/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Simatwa%2Fdjango-pydantic-models/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Simatwa%2Fdjango-pydantic-models/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Simatwa","download_url":"https://codeload.github.com/Simatwa/django-pydantic-models/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Simatwa%2Fdjango-pydantic-models/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30081402,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-04T13:22:36.021Z","status":"ssl_error","status_checked_at":"2026-03-04T13:20:45.750Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-07-11T20:09:02.759Z","updated_at":"2026-03-04T13:30:51.878Z","avatar_url":"https://github.com/Simatwa.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# django-pydantic-models\n\n**A lightweight utility that converts Django models into fully-typed Pydantic models**, supporting automatic field mapping, validation constraints, and nested model generation for related fields (`ForeignKey`, `OneToOneField`, `ManyToManyField`).\n\n[![PyPI version](https://badge.fury.io/py/django-pydantic-models.svg)](https://pypi.org/project/django-pydantic-models/)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)\n\n---\n\n## ✨ Features\n\n- 🔁 Automatic mapping from Django fields to Pydantic types\n- 🧠 Smart support for Django field constraints:\n  - `max_length`, `choices`, `default`, `help_text`, `verbose_name`\n- 📦 Supports nested models for `ForeignKey`, `OneToOneField`, `ManyToManyField`\n- ⚙️ Extensible with Pydantic validators and configuration\n- 🛠️ Add or exclude fields selectively using `__fields__` or `__exclude__`\n- 🚀 Ideal for FastAPI, data validation, or serialization needs\n\n\u003e [!NOTE]\n\u003e This is not a competitor to [djantic](https://github.com/jordaneremieff/djantic) but an optimiser for working around with FastAPI and Django while keeping the traditions - use of decorator - alive.\n\n---\n\n## 📦 Installation\n\n```bash\npip install django-pydantic-models\n````\n\n---\n\n## 🛠 Usage\n\n```python\nfrom django.db import models\nfrom django_pydantic_models import django_model_to_pydantic\n\n\nclass Author(models.Model):\n    name = models.CharField(max_length=100)\n    email = models.EmailField()\n\n\nclass Book(models.Model):\n    title = models.CharField(max_length=200, help_text=\"Title of the book\")\n    published = models.BooleanField(default=False)\n    author = models.ForeignKey(Author, on_delete=models.CASCADE)\n    tags = models.ManyToManyField(\"Tag\")\n\n\nclass Tag(models.Model):\n    name = models.CharField(max_length=50)\n\n\n@django_model_to_pydantic(Book)\nclass BookOut:\n    __fields__ = \"__all__\"  # or list specific fields like ('title', 'author')\n\n    class Config:\n        config = {\"populate_by_name\": True}\n\n    __validators__ = {\n        \"title\": lambda v: v.title()  # Example Pydantic validator\n    }\n```\n\n---\n\n## 🧩 Field Mapping\n\nDjango fields are automatically mapped to their closest Pydantic equivalents. Examples:\n\n| Django Field      | Pydantic Type         |\n| ----------------- | --------------------- |\n| `CharField`       | `str`                 |\n| `EmailField`      | `EmailStr`            |\n| `URLField`        | `HttpUrl`             |\n| `IntegerField`    | `int`                 |\n| `DateTimeField`   | `datetime`            |\n| `ForeignKey`      | nested Pydantic model |\n| `ManyToManyField` | `List[nested model]`  |\n| `choices=`        | `Literal[...]`        |\n\n---\n\n## 🔍 Customization\n\n### Selecting fields\n\n```python\nclass BookOut:\n    __fields__ = ('title', 'author')  # Include only\n    # or use __exclude__ = ('published',)\n```\n\n### Validators\n\n```python\nclass BookOut:\n    __validators__ = {\n        \"title\": lambda v: v.strip().title()\n    }\n```\n\n### Pydantic Config\n\n```python\nclass BookOut:\n    class Config:\n        config = {\n            \"populate_by_name\": True,\n            \"extra\": \"forbid\"\n        }\n```\n\n---\n\n## 🧪 Initialization\n\nModels can be initialized from a Django instance:\n\n```python\nbook = Book.objects.select_related(\"author\").prefetch_related(\"tags\").first()\npydantic_book = BookOut(book)\nprint(pydantic_book.model_dump())\n```\n\nOr with kwargs:\n\n```python\npydantic_book = BookOut(title=\"New Book\", author=AuthorOut(...))\n```\n\n---\n\n\n## 📄 License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n\n---\n\n## 🤝 Contributing\n\nPull requests, issues, and feature suggestions are welcome! Please open an issue or PR.\n\n---\n\n## 📌 Why This Exists\n\nDjango models are great for ORM use but don't offer native support for fully typed external interfaces (e.g., APIs). `django-pydantic-models` bridges this gap, letting you use Django models for database interactions and automatically generate Pydantic models for typed validation and data exchange.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimatwa%2Fdjango-pydantic-models","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimatwa%2Fdjango-pydantic-models","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimatwa%2Fdjango-pydantic-models/lists"}