{"id":24583503,"url":"https://github.com/parrotmac/django-dataclass-field","last_synced_at":"2025-03-17T17:14:36.891Z","repository":{"id":199799873,"uuid":"695300213","full_name":"parrotmac/django-dataclass-field","owner":"parrotmac","description":null,"archived":false,"fork":false,"pushed_at":"2023-10-18T21:49:00.000Z","size":27,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-24T04:46:39.724Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/parrotmac.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-09-22T20:01:48.000Z","updated_at":"2023-09-22T20:03:32.000Z","dependencies_parsed_at":null,"dependency_job_id":"1b94dda8-943e-42bc-aac6-bc007c4ba1f3","html_url":"https://github.com/parrotmac/django-dataclass-field","commit_stats":null,"previous_names":["parrotmac/django-dataclass-field"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parrotmac%2Fdjango-dataclass-field","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parrotmac%2Fdjango-dataclass-field/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parrotmac%2Fdjango-dataclass-field/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parrotmac%2Fdjango-dataclass-field/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/parrotmac","download_url":"https://codeload.github.com/parrotmac/django-dataclass-field/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244075636,"owners_count":20393979,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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-01-24T04:46:51.343Z","updated_at":"2025-03-17T17:14:36.868Z","avatar_url":"https://github.com/parrotmac.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# django-dataclass-field\n\nA simple and efficient way to have a typed Django JSON field using Dataclasses. Under the hood, it leverages the power of `dacite` to bring the magic of dataclasses to your Django models.\n\n## Features\n\n- Use Python's built-in dataclasses as a field type in Django models.\n- Automatic serialization and deserialization using `dacite`.\n- Strongly typed structure for your JSON fields simplifies working with JSON data in Django.\n\n## Installation\n\n### 1. Install the Package\n\nInstall the `django-dataclass-field` package via pip:\n\n```\npip install django-dataclass-field\n```\n\n### 2. Update Django Settings\n\n#### Update `INSTALLED_APPS`\n\nAdd `'django_dataclass_field'` to the `INSTALLED_APPS` list in your Django project's settings:\n\n```python\nINSTALLED_APPS = [\n    ...\n    'django_dataclass_field',\n    ...\n]\n```\n\n#### Update `TEMPLATES` Setting\n\nEnsure that the `APP_DIRS` setting within the `TEMPLATES` configuration is set to `True`. This setting tells Django to look for templates in each app's `templates` directory:\n\n```python\nTEMPLATES = [\n    {\n        ...\n        'APP_DIRS': True,\n        ...\n    },\n]\n```\n\n### 3. Usage\n\nWith the package installed and settings updated, you can now proceed to use `DataClassField` in your Django models. Refer to the \"Usage\" section of this README for detailed examples and guidance.\n\n## Quick Start\n\nLet's consider a car-themed example. Assume you have a dataclass `Car`:\n\n```python\nfrom dataclasses import dataclass\n\n@dataclass\nclass Car:\n    brand: str\n    model: str\n    year: int\n    color: str\n```\n\nNow, if you want to store this data structure in a Django model, you can simply use `DataClassField`:\n\n```python\nfrom django.db import models\nfrom django_dataclass_field.fields import DataClassField\n\nclass CarModel(models.Model):\n    car_data = DataClassField(Car)\n```\n\nWith the above, the `car_data` field will automatically serialize instances of the `Car` dataclass into JSON when saving to the database, and will deserialize the JSON back into an instance of `Car` when reading from the database.\n\nThat's it! You're ready to bring strongly typed JSON fields into your Django projects. Happy coding! 🚗💨\n\n## Usage\n\n### Basic Usage\n\nFollowing the earlier example, you can easily create new instances of `CarModel`:\n\n```python\nnew_car = CarModel(car_data=Car(brand=\"Toyota\", model=\"Prius\", year=2022, color=\"red\"))\nnew_car.save()\n```\n\nRetrieving the instance will give you access to the `Car` dataclass:\n\n```python\nretrieved_car = CarModel.objects.get(id=new_car.id)\nprint(retrieved_car.car_data.brand)  # Outputs: Toyota\n```\n\n### Querying\n\nYou can query the fields as you would with any other Django JSON field. For instance, to find all red Toyotas:\n\n```python\nred_Toyotas = CarModel.objects.filter(car_data__brand=\"Toyota\", car_data__color=\"red\")\n```\n\n## Dependencies\n\n- Django\n- dacite\n\n## License\n\nLicensed under either of\n\n * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)\n * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)\n\nat your option.\n\n## Contribution\n\nWe welcome contributions! Please open an issue or submit a pull request if you have any improvements or features to add.\n\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any\nadditional terms or conditions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparrotmac%2Fdjango-dataclass-field","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fparrotmac%2Fdjango-dataclass-field","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparrotmac%2Fdjango-dataclass-field/lists"}