{"id":21189211,"url":"https://github.com/uptick/django-model-import","last_synced_at":"2025-04-09T21:17:04.250Z","repository":{"id":20751495,"uuid":"89845972","full_name":"uptick/django-model-import","owner":"uptick","description":"Django Model Import -- a fast CSV importer","archived":false,"fork":false,"pushed_at":"2025-02-28T06:09:14.000Z","size":191,"stargazers_count":20,"open_issues_count":3,"forks_count":7,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-04-09T21:16:59.741Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/uptick.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2017-04-30T11:09:35.000Z","updated_at":"2025-02-28T06:09:16.000Z","dependencies_parsed_at":"2025-02-25T04:26:56.405Z","dependency_job_id":"3d457477-3b49-4e45-9082-cd62649cb9d8","html_url":"https://github.com/uptick/django-model-import","commit_stats":null,"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uptick%2Fdjango-model-import","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uptick%2Fdjango-model-import/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uptick%2Fdjango-model-import/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uptick%2Fdjango-model-import/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/uptick","download_url":"https://codeload.github.com/uptick/django-model-import/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248111973,"owners_count":21049578,"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":"2024-11-20T18:50:36.089Z","updated_at":"2025-04-09T21:17:04.227Z","avatar_url":"https://github.com/uptick.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# django-model-import\n\n[![PyPI version](https://badge.fury.io/py/django-model-import.svg)](https://badge.fury.io/py/django-model-import)\n\nDjango Model Import is a light weight CSV importer built for speed.\n\nIt uses a standard Django `ModelForm` to parse each row, giving you a familiar API to work with\nfor data validation and model instantiation. In most cases, if you already have a `ModelForm`\nfor the `ContentType` you are importing you do not need to create an import specific form.\n\nTo present feedback to the end-user running the import you can easily generate a preview\nof the imported data by toggling the `commit` parameter.\n\nIt also provides some import optimized fields for ForeignKey's, allowing preloading all\npossible values, or caching each lookup as it occurs, or looking up a model where multiple\nfields are needed to uniquely identify a resource.\n\n\n## Installation\n\n```bash\npoetry add django-model-import\n```\n\n\n## Quickstart\n\n```python\nimport djangomodelimport\n\nclass BookImporter(djangomodelimport.ImporterModelForm):\n    name = forms.CharField()\n    author = CachedChoiceField(queryset=Author.objects.all(), to_field='name')\n\n    class Meta:\n        model = Book\n        fields = (\n            'name',\n            'author',\n        )\n\nwith default_storage.open('books.csv', 'rb') as fh:\n    data = fh.read().decode(\"utf-8\")\n\n# Use tablib\nparser = djangomodelimport.TablibCSVImportParser(BookImporter)\nheaders, rows = parser.parse(data)\n\n# Process\nimporter = djangomodelimport.ModelImporter(BookImporter)\npreview = importer.process(headers, rows, commit=False)\nerrors = preview.get_errors()\n\nif errors:\n    print(errors)\n\nimportresult = importer.process(headers, rows, commit=True)\nfor result in importresult.get_results():\n    print(result.instance)\n```\n\n\n## Composite key lookups\n\nOften a relationship cannot be referenced via a single unique string. For this we can use\na `CachedChoiceField` with a `CompositeLookupWidget`. The widget looks for the values\nunder the `type` and `variant` columns in the source CSV, and does a unique lookup\nwith the field names specified in `to_field`, e.g. `queryset.get(type__name=type, name=variant)`.\n\nThe results of each `get` are cached internally for the remainder of the import minimising\nany database access.\n\n```python\nclass AssetImporter(ImporterModelForm):\n    site = djangomodelimport.CachedChoiceField(queryset=Site.objects.active(), to_field='ref')\n    type = djangomodelimport.CachedChoiceField(queryset=AssetType.objects.filter(is_active=True), to_field='name')\n    type_variant = djangomodelimport.CachedChoiceField(\n        queryset=InspectionItemTypeVariant.objects.filter(is_active=True),\n        required=False,\n        widget=djangomodelimport.CompositeLookupWidget(source=('type', 'variant')),\n        to_field=('type__name', 'name'),\n    )\n    contractor = djangomodelimport.CachedChoiceField(queryset=Contractor.objects.active(), to_field='name')\n```\n\n\n## Flat related fields\n\nOften you'll have a OneToOneField or just a ForeignKey to another model, but you want to be able to\ncreate/update that other model via this one. You can flatten all of the related model's fields onto\nthis importer using `FlatRelatedField`.\n\n```python\nclass ClientImporter(ImporterModelForm):\n    primary_contact = FlatRelatedField(\n        queryset=ContactDetails.objects.all(),\n        fields={\n            'contact_name': {'to_field': 'name', 'required': True},\n            'email': {'to_field': 'email'},\n            'email_cc': {'to_field': 'email_cc'},\n            'mobile': {'to_field': 'mobile'},\n            'phone_bh': {'to_field': 'phone_bh'},\n            'phone_ah': {'to_field': 'phone_ah'},\n            'fax': {'to_field': 'fax'},\n        },\n    )\n\n    class Meta:\n        model = Client\n        fields = (\n            'name',\n            'ref',\n            'is_active',\n            'account',\n\n            'primary_contact',\n        )\n```\n\n## Tests\nRun tests with `python example/manage.py test testapp`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuptick%2Fdjango-model-import","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuptick%2Fdjango-model-import","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuptick%2Fdjango-model-import/lists"}