{ "info": { "author": "Scott Stafford", "author_email": "scott.stafford+bulksync@gmail.com", "bugtrack_url": null, "classifiers": [ "Framework :: Django", "Framework :: Django :: 1.11", "Framework :: Django :: 2.2", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3" ], "description": "# django-bulk-sync\nCombine bulk create, update, and delete into a single call.\n\n`django-bulk-sync` is a package for the Django ORM that combines bulk_create, bulk_update, and delete into a single method call to `bulk_sync`. \n\nIt manages all necessary creates, updates, and deletes with as few database calls as possible to maximize performance.\n\n## Installation\n\nThe package is available on pip as [django-bulk-sync][django-bulk-sync]. Run:\n\n`pip install django-bulk-sync`\n\nthen import via:\n\n`from bulk_sync import bulk_sync`\n\n## A Usage Scenario\n\nCompanies have zero or more Employees. You want to efficiently sync the names of all employees for a single `Company` from an import from that company, but some are added, updated, or removed. The simple approach is inefficient -- read the import line by line, and:\n\nFor each of N records:\n\n- SELECT to check for the employee's existence\n- UPDATE if it exists, INSERT if it doesn't\n\nThen figure out some way to identify what was missing and delete it. As is so often the case, the speed of this process is controlled mostly by the number of queries run, and here it is about two queries for every record, and so O(N).\n\nInstead, with `bulk_sync`, we can avoid the O(N) number of queries, and simplify the logic we have to write as well. \n\n## Example Usage\n\n```python\nfrom django.db.models import Q\nfrom bulk_sync import bulk_sync\n\nnew_models = []\nfor line in company_import_file:\n\t# The `.id` (or `.pk`) field should not be set. Instead, `key_fields` \n\t# tells it how to match.\n\te = Employee(name=line['name'], phone_number=line['phone_number'], ...)\n\tnew_models.append(e)\n\n# `filters` controls the subset of objects considered when deciding to \n# update or delete.\nfilters = Q(company_id=501) \n# `key_fields` matches an existing object if all `key_fields` are equal.\nkey_fields = ('name', ) \nret = bulk_sync(\n new_models=new_models,\n filters=filters,\n key_fields=key_fields)\n\nprint(\"Results of bulk_sync: \"\n \"{created} created, {updated} updated, {deleted} deleted.\"\n \t\t.format(**ret['stats']))\n```\n\nUnder the hood, it will atomically call `bulk_create`, `bulk_update`, and a single queryset `delete()` call, to correctly and efficiently update all fields of all employees for the filtered Company, using `name` to match properly. \n\n## Argument Reference\n\n`def bulk_sync(new_models, key_fields, filters, batch_size=None):`\nCombine bulk create, update, and delete. Make the DB match a set of in-memory objects.\n- `new_models`: An iterable of Django ORM `Model` objects that you want stored in the database. They may or may not have `id` set, but you should not have already called `save()` on them.\n- `key_fields`: Identifying attribute name(s) to match up `new_models` items with database rows. If a foreign key is being used as a key field, be sure to pass the `fieldname_id` rather than the `fieldname`.\n- `filters`: Q() filters specifying the subset of the database to work in.\n- `batch_size`: passes through to Django `bulk_create.batch_size` and `bulk_update.batch_size`, and controls how many objects are created/updated per SQL query.\n\n`def bulk_compare(old_models, new_models, key_fields, ignore_fields=None):`\nCompare two sets of models by `key_fields`.\n- `old_models`: Iterable of Django ORM objects to compare.\n- `new_models`: Iterable of Django ORM objects to compare.\n- `key_fields`: Identifying attribute name(s) to match up `new_models` items with database rows. If a foreign key\n is being used as a key field, be sure to pass the `fieldname_id` rather than the `fieldname`.\n- `ignore_fields`: (optional) If set, provide field names that should not be considered when comparing objects.\n- Returns dict of: ```\n {\n 'added': list of all added objects.\n 'unchanged': list of all unchanged objects.\n 'updated': list of all updated objects.\n 'updated_details': dict of {obj: {field_name: (old_value, new_value)}} for all changed fields in each updated object.\n 'removed': list of all removed objects.\n } ```\n\n## Frameworks Supported\n\nThis library is tested using Python 3 against Django 1.11 and Django 2.2.\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/mathandpencil/django-bulk-sync", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "django-bulk-sync", "package_url": "https://pypi.org/project/django-bulk-sync/", "platform": "", "project_url": "https://pypi.org/project/django-bulk-sync/", "project_urls": { "Homepage": "https://github.com/mathandpencil/django-bulk-sync" }, "release_url": "https://pypi.org/project/django-bulk-sync/1.3.1/", "requires_dist": null, "requires_python": "", "summary": "Combine bulk add, update, and delete into a single call.", "version": "1.3.1" }, "last_serial": 5568872, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "b29923078bbed2b2f7bd9a30c7003bfc", "sha256": "762e72c375aa54b94bdda0432e03d4376c1bbd66c03a07c9054e76be95c3b092" }, "downloads": -1, "filename": "django_bulk_sync-0.1.0-py3-none-any.whl", "has_sig": false, "md5_digest": "b29923078bbed2b2f7bd9a30c7003bfc", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 4963, "upload_time": "2019-05-17T21:26:49", "url": "https://files.pythonhosted.org/packages/f6/4a/de9d66dcaa9e82a0643c465584212659301086ad1298f491a02353b5268c/django_bulk_sync-0.1.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "b6f3af65355526000f14fb252fcdf484", "sha256": "4254197f53e7ae9dd98cbb57bf0acfd7e3617e4215fed101d1771a0b29cecffb" }, "downloads": -1, "filename": "django-bulk-sync-0.1.0.tar.gz", "has_sig": false, "md5_digest": "b6f3af65355526000f14fb252fcdf484", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3110, "upload_time": "2019-05-17T21:26:51", "url": "https://files.pythonhosted.org/packages/2b/96/ab6aeb91a421f1d7acccebfe47cd215a5a3853cb820a511c43bde522b276/django-bulk-sync-0.1.0.tar.gz" } ], "1.1.0": [ { "comment_text": "", "digests": { "md5": "2eda93b3603ac620f9411b141aad016b", "sha256": "e503a16490d0450e2a9dd886705d175d5d33b142c394ee3a1a9cfb7e3234b979" }, "downloads": -1, "filename": "django_bulk_sync-1.1.0-py3-none-any.whl", "has_sig": false, "md5_digest": "2eda93b3603ac620f9411b141aad016b", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 4277, "upload_time": "2019-05-30T17:06:57", "url": "https://files.pythonhosted.org/packages/c6/c8/371ed391b6a16e14a9d1c168d877608a25d55c4bda9c8508049147aa020d/django_bulk_sync-1.1.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "69a4ab25513000bcbe06a7c2d4c7d89b", "sha256": "425ad473067d1c241cd3f47f9e71d0691e925197d02a3e4c6ff4c8e275d33ac1" }, "downloads": -1, "filename": "django-bulk-sync-1.1.0.tar.gz", "has_sig": false, "md5_digest": "69a4ab25513000bcbe06a7c2d4c7d89b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3223, "upload_time": "2019-05-30T17:06:59", "url": "https://files.pythonhosted.org/packages/a4/c1/8f2bdd4906df03ba55fa44e2935f910bf9934592d2e9eef0a8d27949e802/django-bulk-sync-1.1.0.tar.gz" } ], "1.1.1": [ { "comment_text": "", "digests": { "md5": "f09539cb5ca0e497d53edf0fd7db36d6", "sha256": "00bc9c5be1536c7c220a9815adbcc00e90e71522334d163071174f3ed72a814f" }, "downloads": -1, "filename": "django_bulk_sync-1.1.1-py3-none-any.whl", "has_sig": false, "md5_digest": "f09539cb5ca0e497d53edf0fd7db36d6", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 6174, "upload_time": "2019-05-30T22:13:43", "url": "https://files.pythonhosted.org/packages/ca/81/0b5ff988b63bfe528920f8efad9998555bc4cfcbe04de2f9d2afc5fe457b/django_bulk_sync-1.1.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "cc271a9f8132f3197973c46c0eca4290", "sha256": "7ebe7168ab984b4cc89daeb275935773175eeca65e0f7f488ee70dc1ddb43997" }, "downloads": -1, "filename": "django-bulk-sync-1.1.1.tar.gz", "has_sig": false, "md5_digest": "cc271a9f8132f3197973c46c0eca4290", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4400, "upload_time": "2019-05-30T22:13:45", "url": "https://files.pythonhosted.org/packages/09/8e/2e2cd64da21d464493dfcec14667c06360b649dc72902bc25117117562ea/django-bulk-sync-1.1.1.tar.gz" } ], "1.1.2": [ { "comment_text": "", "digests": { "md5": "de017fa853a84b0671c160e799f990cd", "sha256": "9d63a20cca44fbd49f3ba7b1d30ff0cb3ac5216975d3be3ff6e6dfb83f51833a" }, "downloads": -1, "filename": "django_bulk_sync-1.1.2-py3-none-any.whl", "has_sig": false, "md5_digest": "de017fa853a84b0671c160e799f990cd", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 6516, "upload_time": "2019-06-04T22:00:18", "url": "https://files.pythonhosted.org/packages/32/82/92d8d796af19dddbd57e02dae75ade54a604a4d823805b776086f6da0f91/django_bulk_sync-1.1.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "4db7ee2d49ff18c4dd14e35b55f0ab8f", "sha256": "13e56d5c1aa154d45da8b7063306b00fcae886191111464dd908c56d263b8043" }, "downloads": -1, "filename": "django-bulk-sync-1.1.2.tar.gz", "has_sig": false, "md5_digest": "4db7ee2d49ff18c4dd14e35b55f0ab8f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4665, "upload_time": "2019-06-04T22:00:21", "url": "https://files.pythonhosted.org/packages/3c/e7/70c75648031d1f6258852ea15b90bb57ebc7fc4b8225289a95ce69ba3049/django-bulk-sync-1.1.2.tar.gz" } ], "1.2.0": [ { "comment_text": "", "digests": { "md5": "add45ed121f796558d2345802b7ebdef", "sha256": "d6589a9252c609c28d67c1a753f94fbe33e48bec5005edc5ab71bb1164030e3c" }, "downloads": -1, "filename": "django_bulk_sync-1.2.0-py3-none-any.whl", "has_sig": false, "md5_digest": "add45ed121f796558d2345802b7ebdef", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7739, "upload_time": "2019-06-05T15:11:55", "url": "https://files.pythonhosted.org/packages/2b/44/a38f0cf6bef317f1f84945aefc39c6192bab7b9e239c0727372bb11d4f03/django_bulk_sync-1.2.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "01267e99c0fab51a3ac85e0ac0683e14", "sha256": "6b4b983974e8c70b2c320b90c9f4e4d54bfbda4692753a441b67c37c91231f7f" }, "downloads": -1, "filename": "django-bulk-sync-1.2.0.tar.gz", "has_sig": false, "md5_digest": "01267e99c0fab51a3ac85e0ac0683e14", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5683, "upload_time": "2019-06-05T15:11:59", "url": "https://files.pythonhosted.org/packages/c0/11/d4f6dbd7c735cfc0505d048407ae1a2fb54870283400e737163e3ea20d46/django-bulk-sync-1.2.0.tar.gz" } ], "1.3.0": [ { "comment_text": "", "digests": { "md5": "f1b02871ff5ab6384affa0a43d71a131", "sha256": "3e5b1d45f7b3090ee7bb78905f81aa66c8a3a51d56e50bed5df2da4df4dff49e" }, "downloads": -1, "filename": "django_bulk_sync-1.3.0-py3-none-any.whl", "has_sig": false, "md5_digest": "f1b02871ff5ab6384affa0a43d71a131", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7737, "upload_time": "2019-06-28T19:06:44", "url": "https://files.pythonhosted.org/packages/04/13/9d01fb1d887b63cdc1d6c22c152387a691726515c40b7c60311fabefa410/django_bulk_sync-1.3.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "ed80d75ebce3a98d744f888bf5d37dc1", "sha256": "22ab09b5406344b082867ef98daf4b46eb0a6cb7e19e8c74f8ad111787ae048f" }, "downloads": -1, "filename": "django-bulk-sync-1.3.0.tar.gz", "has_sig": false, "md5_digest": "ed80d75ebce3a98d744f888bf5d37dc1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5688, "upload_time": "2019-06-28T19:06:47", "url": "https://files.pythonhosted.org/packages/41/e7/1db82f99c34090dbdc6b8c909e5962b1eabb26c910c2fd332dced430cc45/django-bulk-sync-1.3.0.tar.gz" } ], "1.3.1": [ { "comment_text": "", "digests": { "md5": "318155681632435f1adae4008a743f9d", "sha256": "3da1fa00d77a4155aed618c719c855f1300111785fd8f80cfa5a7d9e3a48779b" }, "downloads": -1, "filename": "django_bulk_sync-1.3.1-py3-none-any.whl", "has_sig": false, "md5_digest": "318155681632435f1adae4008a743f9d", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7881, "upload_time": "2019-07-22T20:01:53", "url": "https://files.pythonhosted.org/packages/24/d1/1e1ac1e7aa008ce5fc5bbe305645e06822b44c2afce0ea7e0d5cfdbb8f07/django_bulk_sync-1.3.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "69181dcc8177f9d3c0c5948f6ac25047", "sha256": "d69bd171d49f5c89f237800fde929f276fcf3fdf6c5ac1dcbb108223f19ea435" }, "downloads": -1, "filename": "django-bulk-sync-1.3.1.tar.gz", "has_sig": false, "md5_digest": "69181dcc8177f9d3c0c5948f6ac25047", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5832, "upload_time": "2019-07-22T20:01:58", "url": "https://files.pythonhosted.org/packages/34/f7/9bd06baa948516e8275e5a2b8aa42cc0c8836d6907e4177c3bdfc456e573/django-bulk-sync-1.3.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "318155681632435f1adae4008a743f9d", "sha256": "3da1fa00d77a4155aed618c719c855f1300111785fd8f80cfa5a7d9e3a48779b" }, "downloads": -1, "filename": "django_bulk_sync-1.3.1-py3-none-any.whl", "has_sig": false, "md5_digest": "318155681632435f1adae4008a743f9d", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7881, "upload_time": "2019-07-22T20:01:53", "url": "https://files.pythonhosted.org/packages/24/d1/1e1ac1e7aa008ce5fc5bbe305645e06822b44c2afce0ea7e0d5cfdbb8f07/django_bulk_sync-1.3.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "69181dcc8177f9d3c0c5948f6ac25047", "sha256": "d69bd171d49f5c89f237800fde929f276fcf3fdf6c5ac1dcbb108223f19ea435" }, "downloads": -1, "filename": "django-bulk-sync-1.3.1.tar.gz", "has_sig": false, "md5_digest": "69181dcc8177f9d3c0c5948f6ac25047", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5832, "upload_time": "2019-07-22T20:01:58", "url": "https://files.pythonhosted.org/packages/34/f7/9bd06baa948516e8275e5a2b8aa42cc0c8836d6907e4177c3bdfc456e573/django-bulk-sync-1.3.1.tar.gz" } ] }