[![Build Status](https://travis-ci.org/ambitioninc/django-dynamic-initial-data.png)](https://travis-ci.org/ambitioninc/django-dynamic-initial-data)
## Django Initial Data
Django Dynamic Initial Data is a `django>=1.6` and *postgresql* only app that helps solve the problem of initializing data for apps with
dependencies and other conditional data. Rather than having static fixtures for each app, the initial data
can be created and updated dynamically. Furthermore, Django Dynamic Initial Data also handles when objects are
deleted from initial data, a feature that Django's initial data fixture system lacks.
## Table of Contents
1. [Installation] (#installation)
2. [A Brief Overview] (#a-brief-overview)
3. [Example] (#example)
4. [Handling Deletions](#handling-deletions)
## Installation
To install Django Dynamic Initial Data:
```shell
pip install django-dynamic-initial-data
```
Add Django Dynamic Initial Data to your `INSTALLED_APPS` to get started:
settings.py
```python
INSTALLED_APPS = (
'dynamic_initial_data',
)
```
## A Brief Overview
A management command `update_initial_data` is provided which will try to update all `INSTALLED_APPS`. This
command is intended to be called as part of the deployment process of your app. Any missing dependencies
will raise an `InitialDataMissingApp` exception and any circular dependencies will raise an
`InitialDataCircularDependency` exception.
Any app needing to define initial data needs a file called `initial_data.py` inside of a `fixtures`
directory. This will look like `{app_name}/fixtures/initial_data.py`. Don't forget to include
the `__init__.py` file in the fixtures directory. `initial_data.py` must define a class `InitialData`
that inherits from `BaseInitialData`.
When apps are being initialized, each `InitialData` class is instantiated and `update_initial_data` is called.
If `update_initial_data` is not implemented, then a `NotImplementedError` will be raised.
Any dependencies should be included in a list called `dependencies`. Each dependency is a string
of the app name as defined in `INSTALLED_APPS`.
## Example:
```python
from dynamic_initial_data.base import BaseInitialData
class InitialData(BaseInitialData):
dependencies = ['my_first_app', 'my.second.app']
def update_initial_data(self):
model_obj, created = TestModel.objects.upsert(int_field=5, defaults={'float_field': 2.0})
TestModel.objects.bulk_upsert([
TestModel(float_field=1.0, char_field='1', int_field=1),
TestModel(float_field=2.0, char_field='2', int_field=2),
TestModel(float_field=3.0, char_field='3', int_field=3),
], ['int_field'], ['char_field'])
```
In this example, the `update_initial_data` method will be called for `my_first_app` (following any dependencies first),
and then for `my.second.app`, before finally calling `update_initial_data` on this class. Again, this can be executed by calling
```
python manage.py update_initial_data
```
Similarly, to only initialize a single app, use
```
python manage.py update_initial_data --app 'app_path'
```
Documentation on using `upsert` and `bulk_upsert` can be found below:
- https://github.com/ambitioninc/django-manager-utils#upsert
- https://github.com/ambitioninc/django-manager-utils#bulk_upsert
## Handling Deletions
One difficulty when specifying initial data in Django apps is the inability to deploy initial data to your project and then subsequently remove any initial data fixtures. If one removes an object in an initial_data.json file, Django does not handle its deletion next time it is deployed, which can cause headaches with lingering objects.
Django Dynamic Initial Data fixes this problem by allowing the user to either:
1. Return all managed initial data objects as an array from the update_initial_data function.
2. Explicitly register objects for deletion with the register_for_deletion(*model_objs) method.
Note that it is up to the user to be responsible for always registering every object every time, regardless if the object was updated or created by the initial data process. Doing this allows Django Dynamic Initial Data to remove any objects that were previosly managed. For example, assume you have an InitialData class that manages two users with the user names "hello" and "world".
```python
from dynamic_initial_data.base import BaseInitialData
class InitialData(BaseInitialData):
def update_initial_data(self):
hello = Account.objects.get_or_create(name='hello')
world = Account.objects.get_or_create(name='world')
# register the accounts for deletion
self.register_for_deletion(hello, world)
```
After this code is created, the initial data process now owns the "hello" and "world" account objects. If these objects are not registered for deletion in subsequent versions of the code, they will be deleted when the initial data process executes. For example, assume the first piece of code executed and then the user executed this piece of code:
```python
from dynamic_initial_data.base import BaseInitialData
class InitialData(BaseInitialData):
def update_initial_data(self):
world = Account.objects.get_or_create(name='world')
# register the accounts for deletion
self.register_for_deletion(world)
```
When this piece of code executes, the previous "hello" account would then be deleted since the initial data process no longer owns it. And don't worry, if it was already deleted by another process, the deletion will not throw an error.
Raw data
{
"_id": null,
"home_page": "https://github.com/ambitioninc/django-dynamic-initial-data",
"name": "django-dynamic-initial-data",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "Django fixtures",
"author": "",
"author_email": "opensource@ambition.com",
"download_url": "https://files.pythonhosted.org/packages/20/cb/5f8b1c9e27f412d332441d51994a385ab13cb007a389ca09af6506e23c55/django-dynamic-initial-data-2.2.1.tar.gz",
"platform": null,
"description": "[![Build Status](https://travis-ci.org/ambitioninc/django-dynamic-initial-data.png)](https://travis-ci.org/ambitioninc/django-dynamic-initial-data)\n\n## Django Initial Data\n\nDjango Dynamic Initial Data is a `django>=1.6` and *postgresql* only app that helps solve the problem of initializing data for apps with\ndependencies and other conditional data. Rather than having static fixtures for each app, the initial data\ncan be created and updated dynamically. Furthermore, Django Dynamic Initial Data also handles when objects are\ndeleted from initial data, a feature that Django's initial data fixture system lacks.\n\n## Table of Contents\n\n1. [Installation] (#installation)\n2. [A Brief Overview] (#a-brief-overview)\n3. [Example] (#example)\n4. [Handling Deletions](#handling-deletions)\n\n## Installation\nTo install Django Dynamic Initial Data:\n\n```shell\npip install django-dynamic-initial-data\n```\n\nAdd Django Dynamic Initial Data to your `INSTALLED_APPS` to get started:\n\nsettings.py\n```python\nINSTALLED_APPS = (\n 'dynamic_initial_data',\n)\n```\n\n## A Brief Overview\n\nA management command `update_initial_data` is provided which will try to update all `INSTALLED_APPS`. This\ncommand is intended to be called as part of the deployment process of your app. Any missing dependencies\nwill raise an `InitialDataMissingApp` exception and any circular dependencies will raise an\n`InitialDataCircularDependency` exception.\n\nAny app needing to define initial data needs a file called `initial_data.py` inside of a `fixtures`\ndirectory. This will look like `{app_name}/fixtures/initial_data.py`. Don't forget to include\nthe `__init__.py` file in the fixtures directory. `initial_data.py` must define a class `InitialData`\nthat inherits from `BaseInitialData`.\n\nWhen apps are being initialized, each `InitialData` class is instantiated and `update_initial_data` is called.\nIf `update_initial_data` is not implemented, then a `NotImplementedError` will be raised.\n\nAny dependencies should be included in a list called `dependencies`. Each dependency is a string\nof the app name as defined in `INSTALLED_APPS`.\n\n## Example:\n\n```python\nfrom dynamic_initial_data.base import BaseInitialData\n\nclass InitialData(BaseInitialData):\n dependencies = ['my_first_app', 'my.second.app']\n\n def update_initial_data(self):\n model_obj, created = TestModel.objects.upsert(int_field=5, defaults={'float_field': 2.0})\n\n TestModel.objects.bulk_upsert([\n TestModel(float_field=1.0, char_field='1', int_field=1),\n TestModel(float_field=2.0, char_field='2', int_field=2),\n TestModel(float_field=3.0, char_field='3', int_field=3),\n ], ['int_field'], ['char_field'])\n```\nIn this example, the `update_initial_data` method will be called for `my_first_app` (following any dependencies first),\nand then for `my.second.app`, before finally calling `update_initial_data` on this class. Again, this can be executed by calling\n\n```\npython manage.py update_initial_data\n```\n\nSimilarly, to only initialize a single app, use\n\n```\npython manage.py update_initial_data --app 'app_path'\n```\n\nDocumentation on using `upsert` and `bulk_upsert` can be found below:\n- https://github.com/ambitioninc/django-manager-utils#upsert\n- https://github.com/ambitioninc/django-manager-utils#bulk_upsert\n\n## Handling Deletions\nOne difficulty when specifying initial data in Django apps is the inability to deploy initial data to your project and then subsequently remove any initial data fixtures. If one removes an object in an initial_data.json file, Django does not handle its deletion next time it is deployed, which can cause headaches with lingering objects.\n\nDjango Dynamic Initial Data fixes this problem by allowing the user to either:\n\n1. Return all managed initial data objects as an array from the update_initial_data function.\n2. Explicitly register objects for deletion with the register_for_deletion(*model_objs) method.\n\nNote that it is up to the user to be responsible for always registering every object every time, regardless if the object was updated or created by the initial data process. Doing this allows Django Dynamic Initial Data to remove any objects that were previosly managed. For example, assume you have an InitialData class that manages two users with the user names \"hello\" and \"world\".\n\n```python\nfrom dynamic_initial_data.base import BaseInitialData\n\nclass InitialData(BaseInitialData):\n def update_initial_data(self):\n hello = Account.objects.get_or_create(name='hello')\n world = Account.objects.get_or_create(name='world')\n # register the accounts for deletion\n self.register_for_deletion(hello, world)\n```\n\nAfter this code is created, the initial data process now owns the \"hello\" and \"world\" account objects. If these objects are not registered for deletion in subsequent versions of the code, they will be deleted when the initial data process executes. For example, assume the first piece of code executed and then the user executed this piece of code:\n\n```python\nfrom dynamic_initial_data.base import BaseInitialData\n\nclass InitialData(BaseInitialData):\n def update_initial_data(self):\n world = Account.objects.get_or_create(name='world')\n # register the accounts for deletion\n self.register_for_deletion(world)\n```\n\nWhen this piece of code executes, the previous \"hello\" account would then be deleted since the initial data process no longer owns it. And don't worry, if it was already deleted by another process, the deletion will not throw an error.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Dynamic initial data fixtures for Django apps",
"version": "2.2.1",
"project_urls": {
"Homepage": "https://github.com/ambitioninc/django-dynamic-initial-data"
},
"split_keywords": [
"django",
"fixtures"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "3882e9c0b956e6cddd35d3fd5ac082a08af40efc6c5280efc0ccacf8085cb78e",
"md5": "35c8a21fbeac85f4272ecbb1d60287d3",
"sha256": "b500aaff5507607ae91b1a65830d9685ddb3d4851bca1e4a535f132337b1bdb2"
},
"downloads": -1,
"filename": "django_dynamic_initial_data-2.2.1-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "35c8a21fbeac85f4272ecbb1d60287d3",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 18796,
"upload_time": "2023-06-29T13:40:40",
"upload_time_iso_8601": "2023-06-29T13:40:40.283064Z",
"url": "https://files.pythonhosted.org/packages/38/82/e9c0b956e6cddd35d3fd5ac082a08af40efc6c5280efc0ccacf8085cb78e/django_dynamic_initial_data-2.2.1-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "20cb5f8b1c9e27f412d332441d51994a385ab13cb007a389ca09af6506e23c55",
"md5": "66272ba4099dfd3a991b46bdfa2c46a3",
"sha256": "347d5bc7058427d6d3ef01fda86dde96d19428e2685bee712903aaaab13ec8ac"
},
"downloads": -1,
"filename": "django-dynamic-initial-data-2.2.1.tar.gz",
"has_sig": false,
"md5_digest": "66272ba4099dfd3a991b46bdfa2c46a3",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 13909,
"upload_time": "2023-06-29T13:40:42",
"upload_time_iso_8601": "2023-06-29T13:40:42.103464Z",
"url": "https://files.pythonhosted.org/packages/20/cb/5f8b1c9e27f412d332441d51994a385ab13cb007a389ca09af6506e23c55/django-dynamic-initial-data-2.2.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-06-29 13:40:42",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ambitioninc",
"github_project": "django-dynamic-initial-data",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"lcname": "django-dynamic-initial-data"
}