{ "info": { "author": "S. Andrew Sheppard", "author_email": "andrew@wq.io", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Environment :: Web Environment", "Framework :: Django", "Framework :: Django :: 1.10", "Framework :: Django :: 1.11", "Framework :: Django :: 1.6", "Framework :: Django :: 1.7", "Framework :: Django :: 1.8", "Framework :: Django :: 1.9", "Framework :: Django :: 2.0", "Framework :: Django :: 2.1", "Framework :: Django :: 2.2", "License :: OSI Approved :: MIT License", "Natural Language :: English", "Operating System :: OS Independent", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7" ], "description": "Swapper\n=======\n\n#### Django Swappable Models - No longer only for auth.User!\n\nSwapper is an unofficial API for the [undocumented] but very powerful Django \nfeature: swappable models. Swapper facilitates implementing\narbitrary swappable models in your own reusable apps.\n\n[![Latest PyPI Release](https://img.shields.io/pypi/v/swapper.svg)](https://pypi.org/project/swapper)\n[![Release Notes](https://img.shields.io/github/release/wq/django-swappable-models.svg\n)](https://github.com/wq/django-swappable-models/releases)\n[![License](https://img.shields.io/pypi/l/swapper.svg)](https://github.com/wq/django-swappable-models/blob/master/LICENSE)\n[![GitHub Stars](https://img.shields.io/github/stars/wq/django-swappable-models.svg)](https://github.com/wq/django-swappable-models/stargazers)\n[![GitHub Forks](https://img.shields.io/github/forks/wq/django-swappable-models.svg)](https://github.com/wq/django-swappable-models/network)\n[![GitHub Issues](https://img.shields.io/github/issues/wq/django-swappable-models.svg)](https://github.com/wq/django-swappable-models/issues)\n\n[![Travis Build Status](https://img.shields.io/travis/wq/django-swappable-models.svg)](https://travis-ci.org/wq/django-swappable-models)\n[![Python Support](https://img.shields.io/pypi/pyversions/swapper.svg)](https://pypi.org/project/swapper)\n[![Django Support](https://img.shields.io/pypi/djversions/swapper.svg)](https://pypi.org/project/swapper)\n\n## Motivation\n\nSuppose your reusable app has two related tables:\n\n```python\nfrom django.db import models\nclass Parent(models.Model):\n name = models.TextField()\n\nclass Child(models.Model):\n name = models.TextField()\n parent = models.ForeignKey(Parent)\n```\n\nSuppose further that you want to allow the user to subclass either or both of\nthese models and supplement them with their own additional fields. You could use\nAbstract classes (e.g. `BaseParent` and `BaseChild`) for this, but then you\nwould either need to:\n\n 1. Avoid putting the foreign key on `BaseChild` and tell the user they need to\n do it.\n 2. Put the foreign key on `BaseChild`, but make `Parent` a concrete model that\n can't be swapped\n 3. Use swappable models, together with `ForeignKeys` that read the swappable\n settings.\n\nThis third approach is taken by Django to facilitate [swapping the auth.User model]. The `auth.User` swappable code was implemented in a generic way that allows it to be used for any model. Although this capability is currently [undocumented] while any remaining issues are being sorted out, it has proven to be very stable and useful in our experience.\n\nSwapper is essentially a simple API wrapper around this existing functionality. Note that Swapper is primarily a tool for library authors; users of your reusable app generally should not need to know about Swapper in order to use it. (See the notes on [End User Documentation](#end-user-documentation) below.)\n\n### Real-World Example\nSwapper is used extensively in the [vera] extension to [wq.db]. vera provides [7 inter-related models], each of which can be swapped out for custom implementations. (Swapper actually started out as part of [wq.db.patterns], but was extracted for more general-purpose use.)\n\n## Creating a Reusable App\n\nFirst, make sure you have `swapper` installed. If you are publishing your reusable app as a Python package, be sure to add `swapper` to your project's dependencies (e.g. `setup.py`) to ensure that users of your app don't have errors integrating it.\n\n```bash\npip3 install swapper\n```\nExtending the above example, you might create two abstract base classes and corresponding default implementations:\n\n```python\n# reusableapp/models.py\nfrom django.db import models\nimport swapper\n\nclass BaseParent(models.Model):\n # minimal base implementation ...\n class Meta:\n abstract = True\n\nclass Parent(BaseParent):\n # default (swappable) implementation ...\n class Meta:\n swappable = swapper.swappable_setting('reusableapp', 'Parent')\n\nclass BaseChild(models.Model):\n parent = models.ForeignKey(swapper.get_model_name('reusableapp', 'Parent'))\n # minimal base implementation ...\n class Meta:\n abstract = True\n\nclass Child(BaseChild):\n # default (swappable) implementation ...\n class Meta:\n swappable = swapper.swappable_setting('reusableapp', 'Child')\n```\n\n### Loading Swapped Models\n\nIn your reusable views and other functions, always use the swapper instead of importing swappable models directly. This is because you might not know whether the user of your app is using your default implementation or their own version.\n\n```python\n# reusableapp/views.py\n\n# Might work, might not\n# from .models import Parent\n\nimport swapper\nParent = swapper.load_model(\"reusableapp\", \"Parent\")\nChild = swapper.load_model(\"reusableapp\", \"Child\")\n\ndef view(request, *args, **kwargs):\n qs = Parent.objects.all()\n # ...\n```\n\n> Note: `swapper.load_model()` is the general equivalent of [get_user_model()] and subject to the same constraints: e.g. it should not be used until after the model system has fully initialized.\n\n### Migration Scripts\nSwapper can also be used in Django 1.7+ migration scripts to facilitate dependency ordering and foreign key references. To use this feature in your library, generate a migration script with `makemigrations` and make the following changes. In general, users of your library should not need to make any similar changes to their own migration scripts. The one exception is if you have multiple levels of swappable models with foreign keys pointing to each other (as in [vera]).\n\n```diff\n # reusableapp/migrations/0001_initial.py\n\n from django.db import models, migrations\n< from django.conf import settings\n> import swapper\n\n class Migration(migrations.Migration):\n\n dependencies = [\n< migrations.swappable_dependency(settings.REUSABLEAPP_PARENT_MODEL),\n> swapper.dependency('reusableapp', 'Parent')\n ]\n\n operations = [\n migrations.CreateModel(\n name='Child',\n fields=[\n ('id', models.AutoField(auto_created=True, serialize=False, primary_key=True, verbose_name='ID')),\n ],\n options={\n< 'swappable': 'REUSABLEAPP_CHILD_MODEL',\n> 'swappable': swapper.swappable_setting('reusableapp', 'Child'),\n },\n bases=(models.Model,),\n ),\n migrations.CreateModel(\n name='Parent',\n fields=[\n ('id', models.AutoField(auto_created=True, serialize=False, primary_key=True, verbose_name='ID')),\n ],\n options={\n< 'swappable': 'REUSABLEAPP_PARENT_MODEL',\n> 'swappable': swapper.swappable_setting('reusableapp', 'Parent'),\n },\n bases=(models.Model,),\n ),\n migrations.AddField(\n model_name='child',\n name='parent',\n< field=models.ForeignKey(to=settings.REUSABLEAPP_PARENT_MODEL),\n> field=models.ForeignKey(to=swapper.get_model_name('reusableapp', 'Parent')),\n preserve_default=True,\n ),\n ]\n```\n\n## End User Documentation\nWith the above setup, the user of your app can override one or both models in their own app. You might provide them with an example like this:\n\n```python\n# myapp/models.py\nfrom reusableapp.models import BaseParent\nclass Parent(BaseParent):\n # custom implementation ...\n```\n\nThen, tell your users to update their settings to trigger the swap.\n\n```python\n# myproject/settings.py\nREUSABLEAPP_PARENT_MODEL = \"myapp.Parent\"\n```\n\nThe goal is to make this process just as easy for your end user as [swapping the auth.User model] is. As with `auth.User`, there are some important caveats that you may want to inform your users about.\n\nThe biggest issue is that your users will probably need to define the swapped model settings **before creating any migrations** for their implementation of `myapp`. Due to key assumptions made within Django's migration infrastructure, it is difficult to start out with a default (non-swapped) model and then later to switch to a swapped implementation without doing some migration hacking. This is somewhat awkward - as your users will most likely want to try out your default implementation before deciding to customize it. Unfortunately, there isn't an easy workaround due to how the swappable setting is currently implemented in Django core. This will likely be addressed in future Django versions (see [#10] and [Django ticket #25313]).\n\n## API Documentation\n\nHere is the full API for `swapper`, which you may find useful in creating your reusable app code. End users of your library should generally not need to reference this API.\n\nfunction | purpose\n---------|--------\n`swappable_setting(app_label, model)` | Generates a swappable setting name for the provided model (e.g. `\"REUSABLEAPP_PARENT_MODEL\"`)\n`is_swapped(app_label, model)` | Determines whether or not a given model has been swapped. (Returns the model name if swapped, otherwise `False`)\n`get_model_name(app_label, model)` | Gets the name of the model the swappable model has been swapped for (or the name of the original model if not swapped.)\n`get_model_names(app_label, models)` | Match a list of model names to their swapped versions. All of the models should be from the same app (though their swapped versions need not be).\n`load_model(app_label, model, required=True)` | Load the swapped model class for a swappable model (or the original model if it hasn't been swapped). If your code can function without the specified model, set `required = False`.\n`dependency(app_label, model)` | Generate a dependency tuple for use in Django 1.7+ migrations.\n`set_app_prefix(app_label, prefix)` | Set a custom prefix for swappable settings (the default is the upper case `app_label`). Used in [wq.db] to make all of the swappable settings start with `\"WQ\"` (e.g. `WQ_FILE_MODEL` instead of `FILES_FILE_MODEL`). This should be set at the top of your models.py.\n`join(app_label, model)`, `split(model)` | Utilities for splitting and joining `\"app.Model\"` strings and `(\"app\", \"Model\")` tuples.\n\n[undocumented]: https://code.djangoproject.com/ticket/19103\n[swapping the auth.User model]: https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#auth-custom-user\n[wq.db]: http://wq.io/wq.db\n[vera]: http://wq.io/vera\n[wq.db.patterns]: http://wq.io/docs/about-patterns\n[7 inter-related models]: https://github.com/wq/vera#models\n[get_user_model()]: https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#referencing-the-user-model\n[#10]: https://github.com/wq/django-swappable-models/issues/10\n[Django ticket #25313]: https://code.djangoproject.com/ticket/25313\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/wq/django-swappable-models", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "swapper", "package_url": "https://pypi.org/project/swapper/", "platform": "", "project_url": "https://pypi.org/project/swapper/", "project_urls": { "Homepage": "https://github.com/wq/django-swappable-models" }, "release_url": "https://pypi.org/project/swapper/1.1.1/", "requires_dist": null, "requires_python": "", "summary": "The unofficial Django swappable models API.", "version": "1.1.1" }, "last_serial": 5570158, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "01a9d591effc057ff30067ae54c53097", "sha256": "02baad60bf34dac9e1c82c9def4313b22e4e8c7e562b29fcf9061551e3010264" }, "downloads": -1, "filename": "swapper-0.1.0.tar.gz", "has_sig": false, "md5_digest": "01a9d591effc057ff30067ae54c53097", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3450, "upload_time": "2014-01-09T01:35:48", "url": "https://files.pythonhosted.org/packages/30/cf/5729481e28efd66db61e404bdc0e02bd4827a18654be9bd81cefe0e18f54/swapper-0.1.0.tar.gz" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "1efc03c4b4ec45232b490681873e879e", "sha256": "267f1aa0f34f0e1e95e294ad6e7c25248e7c4915fc4ad2ad4ff88d06eb83be95" }, "downloads": -1, "filename": "swapper-0.1.1.tar.gz", "has_sig": false, "md5_digest": "1efc03c4b4ec45232b490681873e879e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3811, "upload_time": "2014-01-09T17:45:33", "url": "https://files.pythonhosted.org/packages/c2/db/72c9566d612c6aa452c3e07dc032f918c52e858ca4cb48dfab0981196641/swapper-0.1.1.tar.gz" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "c36e92d23188c20c9e9a71cb296609cc", "sha256": "828c930d3600d1a44c0cb6204783fdfcc31b0d12eb78e358bbb93cd68d0f5131" }, "downloads": -1, "filename": "swapper-0.2.0.tar.gz", "has_sig": false, "md5_digest": "c36e92d23188c20c9e9a71cb296609cc", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4083, "upload_time": "2014-09-12T22:02:12", "url": "https://files.pythonhosted.org/packages/3c/90/fce87c2ad102477996554dfa436e055d25bb265947e938391a7b1b9b2483/swapper-0.2.0.tar.gz" } ], "0.2.1": [ { "comment_text": "", "digests": { "md5": "d04f05b3620dbf8aa90af8a27c2a42bf", "sha256": "76a43fb60b1c2c41a0a5ab327423535c80f03e737dd613d83234fba9593e9aaf" }, "downloads": -1, "filename": "swapper-0.2.1.tar.gz", "has_sig": false, "md5_digest": "d04f05b3620dbf8aa90af8a27c2a42bf", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5554, "upload_time": "2014-11-17T19:48:11", "url": "https://files.pythonhosted.org/packages/ea/3e/c66d6e5607f1c1eb7e5759532cb6b239b5e483630d03ec492322d08f3247/swapper-0.2.1.tar.gz" } ], "0.2.2": [ { "comment_text": "", "digests": { "md5": "3b057b8a3ed7c444decdc26068a63d75", "sha256": "411dfbdb52f73505f01c8e9611111d364bb0df41d6bb8e902dc1d86c61d07049" }, "downloads": -1, "filename": "swapper-0.2.2.tar.gz", "has_sig": false, "md5_digest": "3b057b8a3ed7c444decdc26068a63d75", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6134, "upload_time": "2015-06-15T20:15:35", "url": "https://files.pythonhosted.org/packages/6c/77/3a6a7efe5c5c52579748eb29e5b20d8cabdcb34e7d7fd0241615835178b1/swapper-0.2.2.tar.gz" } ], "0.3.0": [ { "comment_text": "", "digests": { "md5": "3b6b10bda1bc30d4f7f928a2046565ae", "sha256": "55bd1fb3aa61d1e18e9f3582da086cc30975d4788499d151326f2fcebdbbbcc0" }, "downloads": -1, "filename": "swapper-0.3.0.tar.gz", "has_sig": false, "md5_digest": "3b6b10bda1bc30d4f7f928a2046565ae", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6245, "upload_time": "2015-11-16T20:33:27", "url": "https://files.pythonhosted.org/packages/33/fe/b45eb38e066fa999d4e59770642b1d50069280bb8341d3d24c64973d01cd/swapper-0.3.0.tar.gz" } ], "1.0.0": [ { "comment_text": "", "digests": { "md5": "d5bcbdd8ad0c6c3c7278bc689b0d0b90", "sha256": "907861b223844d6fa77669f96f2eda96303002d69b4ea941f447fa08d43cc19c" }, "downloads": -1, "filename": "swapper-1.0.0.tar.gz", "has_sig": false, "md5_digest": "d5bcbdd8ad0c6c3c7278bc689b0d0b90", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7340, "upload_time": "2016-08-26T18:09:11", "url": "https://files.pythonhosted.org/packages/23/c1/1147572c0094e5d1f452ef6457ed7a95dee3b159baabadb1dde2bc404d10/swapper-1.0.0.tar.gz" } ], "1.1.0": [ { "comment_text": "", "digests": { "md5": "f177707043a81c973fc071252546b5ff", "sha256": "bcd162c85d3906f6aabd3970e854041b4327e67d0fc96d07e234a85d96131425" }, "downloads": -1, "filename": "swapper-1.1.0.tar.gz", "has_sig": false, "md5_digest": "f177707043a81c973fc071252546b5ff", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8046, "upload_time": "2017-05-11T02:06:33", "url": "https://files.pythonhosted.org/packages/98/a1/44d48ab9e004eb64bb429f8421986ac1c4d6e48c38cd98bb9e0ff543b116/swapper-1.1.0.tar.gz" } ], "1.1.1": [ { "comment_text": "", "digests": { "md5": "a026a5ffd1bb8ace0693cf6f1425f82e", "sha256": "a5cf78bb9b5fcf458188067bb4fbcc248d4224205a3d1a54d1b835429b0d72c9" }, "downloads": -1, "filename": "swapper-1.1.1-py3-none-any.whl", "has_sig": false, "md5_digest": "a026a5ffd1bb8ace0693cf6f1425f82e", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 6910, "upload_time": "2019-07-23T03:00:10", "url": "https://files.pythonhosted.org/packages/89/91/d3a37c8c24745d3a35fa13dbf9a7767c828e7db4c87e7e4e71779a952b87/swapper-1.1.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "13871940b4acbd197db6aaba913b2679", "sha256": "bc0928ad9badbd7a445588468e4c956ddf9d0dabc2ca27ea2cc73f646cb892b0" }, "downloads": -1, "filename": "swapper-1.1.1.tar.gz", "has_sig": false, "md5_digest": "13871940b4acbd197db6aaba913b2679", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10291, "upload_time": "2019-07-23T03:00:11", "url": "https://files.pythonhosted.org/packages/00/04/fac586dc96ed080cd547e3367339d99363130d85cc60e878f704cd0b290b/swapper-1.1.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "a026a5ffd1bb8ace0693cf6f1425f82e", "sha256": "a5cf78bb9b5fcf458188067bb4fbcc248d4224205a3d1a54d1b835429b0d72c9" }, "downloads": -1, "filename": "swapper-1.1.1-py3-none-any.whl", "has_sig": false, "md5_digest": "a026a5ffd1bb8ace0693cf6f1425f82e", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 6910, "upload_time": "2019-07-23T03:00:10", "url": "https://files.pythonhosted.org/packages/89/91/d3a37c8c24745d3a35fa13dbf9a7767c828e7db4c87e7e4e71779a952b87/swapper-1.1.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "13871940b4acbd197db6aaba913b2679", "sha256": "bc0928ad9badbd7a445588468e4c956ddf9d0dabc2ca27ea2cc73f646cb892b0" }, "downloads": -1, "filename": "swapper-1.1.1.tar.gz", "has_sig": false, "md5_digest": "13871940b4acbd197db6aaba913b2679", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10291, "upload_time": "2019-07-23T03:00:11", "url": "https://files.pythonhosted.org/packages/00/04/fac586dc96ed080cd547e3367339d99363130d85cc60e878f704cd0b290b/swapper-1.1.1.tar.gz" } ] }