# Django Smart Fixtures
**DON'T BE STUPID, AND LOAD FIXTURES IN A SMART WAY!**
## Purpose
This Django package extends Django's `loaddata` management command allowing you
to **load fixtures in a more convenient way**. Unlike original `loaddata`,
this command allows you to load multiple fixtures without passing their labels
to the command. The only thing you need to do is to configure the fixtures to
load in the settings. Ah, yes... and it also allows you to **easily upload media
files** (images, files, etc.) from the fixtures.
## Installation
```bash
pip install django-smart-fixtures
```
## Configuration
Add `smart_fixtures` to your `INSTALLED_APPS`:
```python
INSTALLED_APPS = [
...
'smart_fixtures',
...
]
```
### `FIXTURES` settings configuration
Let's say you have the following fixtures:
```plaintext
my_app/
└──fixtures/
├── fixtures1.yaml
├── fixtures2.yaml
└── images/
├── image1.jpg
└── image2.jpg
my_other_app/
└──fixtures/
├── fixtures3.yaml
└── files/
├── file1.txt
└── file2.txt
```
You can configure the fixtures to load in the settings:
```python
# settings.py
FIXTURES = {
'labels': [
'fixtures1',
'fixtures2',
'fixtures3',
],
'media': [
{
'src': BASE_DIR / 'my_app' / 'fixtures' / 'images',
'dest': MEDIA_ROOT / 'my_app' / 'images',
},
{
'src': BASE_DIR / 'my_other_app' / 'fixtures' / 'files',
'dest': MEDIA_ROOT / 'my_other_app' / 'files',
},
],
}
```
## Usage
Load fixtures configured in the `FIXTURES` settings by running the following
command:
```bash
python manage.py loaddata --all
```
Using the above example, running this command will load all the fixtures defined
in `fixtures1.yaml`, `fixtures2.yaml`, and `fixtures3.yaml`. It will also copy
all files from `images` and `files` folders to the media folder.
## Defining fixtures for models with file fields
When defining paths to media files in the fixture files, you should use paths
relative to the media root directory. The media root directory is defined by the
`MEDIA_ROOT` setting. The paths should be defined in the following way:
```yaml
- model: my_app.MyModel
pk: 1
fields:
image: my_app/images/image1.jpg
```
Using the above example, files from `images` will end up in the
`{media_root}/my_app/images` folder, and files from `files` will end up in the
`{media_root}/my_other_app/files` folder. Relative to media root directory,
paths of copied files will be:
- `my_app/images/image1.jpg`
- `my_app/images/image2.jpg`
- `my_other_app/files/file1.txt`
- `my_other_app/files/file2.txt`
The above paths should be used in the fixture files for the `models.ImageField`,
`models.FileField`, and other file fields.
### Media files configuration
Make sure to set the `MEDIA_ROOT` and `MEDIA_URL` settings in your Django
project. The `MEDIA_ROOT` setting should point to the directory where the media
files will be stored. The `MEDIA_URL` setting should point to the URL that will
be used to serve the media files.
```python
# settings.py
MEDIA_ROOT = BASE_DIR / 'media'
MEDIA_URL = '/media/'
```
Also, make sure to add the `MEDIA_URL` to the `urlpatterns` in the `urls.py`:
```python
# urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
...
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
```
## YAML fixtures
If you're defining fixtures in YAML files, make sure to use `.yaml` extension
instead of `.yml` for the fixture files because Django's `loaddata` command does
not support `.yml` files.
## Publishing to PyPI
To publish the package to PyPI, follow these steps:
1. Update the version in the `pyproject.toml` file.
2. Build the package:
```bash
poetry build
```
3. Configure your PyPI credentials (if you haven't already):
```bash
poetry config pypi-token.pypi <your-pypi-token>
```
4. Publish the package:
```bash
poetry publish
```
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
for details.
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request or open an
Issue.
Raw data
{
"_id": null,
"home_page": "https://github.com/roknicmilos/django-smart-fixtures",
"name": "django-smart-fixtures",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": "fixtures, loaddata, load fixtures, smart fixtures",
"author": "Milo\u0161 Rokni\u0107",
"author_email": "roknic.milos.994@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/39/15/22702c590da5fdf182f34b1b516d02eb02432af319cc5b507611b2ce3006/django_smart_fixtures-2.0.3.tar.gz",
"platform": null,
"description": "# Django Smart Fixtures\n\n**DON'T BE STUPID, AND LOAD FIXTURES IN A SMART WAY!**\n\n## Purpose\n\nThis Django package extends Django's `loaddata` management command allowing you\nto **load fixtures in a more convenient way**. Unlike original `loaddata`,\nthis command allows you to load multiple fixtures without passing their labels\nto the command. The only thing you need to do is to configure the fixtures to\nload in the settings. Ah, yes... and it also allows you to **easily upload media\nfiles** (images, files, etc.) from the fixtures.\n\n## Installation\n\n```bash\npip install django-smart-fixtures\n```\n\n## Configuration\n\nAdd `smart_fixtures` to your `INSTALLED_APPS`:\n\n```python\nINSTALLED_APPS = [\n ...\n 'smart_fixtures',\n ...\n]\n```\n\n### `FIXTURES` settings configuration\n\nLet's say you have the following fixtures:\n\n```plaintext\nmy_app/\n\u2514\u2500\u2500fixtures/\n \u251c\u2500\u2500 fixtures1.yaml\n \u251c\u2500\u2500 fixtures2.yaml\n \u2514\u2500\u2500 images/\n \u251c\u2500\u2500 image1.jpg\n \u2514\u2500\u2500 image2.jpg\n \nmy_other_app/\n\u2514\u2500\u2500fixtures/\n \u251c\u2500\u2500 fixtures3.yaml\n \u2514\u2500\u2500 files/\n \u251c\u2500\u2500 file1.txt\n \u2514\u2500\u2500 file2.txt\n```\n\nYou can configure the fixtures to load in the settings:\n\n```python\n# settings.py\nFIXTURES = {\n 'labels': [\n 'fixtures1',\n 'fixtures2',\n 'fixtures3',\n ],\n 'media': [\n {\n 'src': BASE_DIR / 'my_app' / 'fixtures' / 'images',\n 'dest': MEDIA_ROOT / 'my_app' / 'images',\n },\n {\n 'src': BASE_DIR / 'my_other_app' / 'fixtures' / 'files',\n 'dest': MEDIA_ROOT / 'my_other_app' / 'files',\n },\n ],\n}\n```\n\n## Usage\n\nLoad fixtures configured in the `FIXTURES` settings by running the following\ncommand:\n\n```bash\npython manage.py loaddata --all\n```\n\nUsing the above example, running this command will load all the fixtures defined\nin `fixtures1.yaml`, `fixtures2.yaml`, and `fixtures3.yaml`. It will also copy\nall files from `images` and `files` folders to the media folder.\n\n## Defining fixtures for models with file fields\n\nWhen defining paths to media files in the fixture files, you should use paths\nrelative to the media root directory. The media root directory is defined by the\n`MEDIA_ROOT` setting. The paths should be defined in the following way:\n\n```yaml\n- model: my_app.MyModel\n pk: 1\n fields:\n image: my_app/images/image1.jpg\n```\n\nUsing the above example, files from `images` will end up in the\n`{media_root}/my_app/images` folder, and files from `files` will end up in the\n`{media_root}/my_other_app/files` folder. Relative to media root directory,\npaths of copied files will be:\n\n- `my_app/images/image1.jpg`\n- `my_app/images/image2.jpg`\n- `my_other_app/files/file1.txt`\n- `my_other_app/files/file2.txt`\n\nThe above paths should be used in the fixture files for the `models.ImageField`,\n`models.FileField`, and other file fields.\n\n### Media files configuration\n\nMake sure to set the `MEDIA_ROOT` and `MEDIA_URL` settings in your Django\nproject. The `MEDIA_ROOT` setting should point to the directory where the media\nfiles will be stored. The `MEDIA_URL` setting should point to the URL that will\nbe used to serve the media files.\n\n```python\n# settings.py\nMEDIA_ROOT = BASE_DIR / 'media'\nMEDIA_URL = '/media/'\n```\n\nAlso, make sure to add the `MEDIA_URL` to the `urlpatterns` in the `urls.py`:\n\n```python\n# urls.py\nfrom django.conf import settings\nfrom django.conf.urls.static import static\n\nurlpatterns = [\n ...\n]\nurlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)\n```\n\n## YAML fixtures\n\nIf you're defining fixtures in YAML files, make sure to use `.yaml` extension\ninstead of `.yml` for the fixture files because Django's `loaddata` command does\nnot support `.yml` files.\n\n## Publishing to PyPI\n\nTo publish the package to PyPI, follow these steps:\n\n1. Update the version in the `pyproject.toml` file.\n2. Build the package:\n ```bash\n poetry build\n ```\n3. Configure your PyPI credentials (if you haven't already):\n\n ```bash\n poetry config pypi-token.pypi <your-pypi-token>\n ```\n4. Publish the package:\n\n ```bash\n poetry publish\n ```\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file\nfor details.\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request or open an\nIssue.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Don't be stupid, and load fixtures in a smart way",
"version": "2.0.3",
"project_urls": {
"Documentation": "https://github.com/roknicmilos/django-smart-fixtures/blob/main/README.md",
"Homepage": "https://github.com/roknicmilos/django-smart-fixtures",
"Repository": "https://github.com/roknicmilos/django-smart-fixtures"
},
"split_keywords": [
"fixtures",
" loaddata",
" load fixtures",
" smart fixtures"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d5731a76b9068659efc824df4a11a5582280fa048347503633fc5bddca10fa35",
"md5": "9cdeae2fbbc844c98212f99ea86f45b0",
"sha256": "268ac517f7d1bc800fa761188e254cb5d4b6f34c5213842250bf1bd15ef16cd1"
},
"downloads": -1,
"filename": "django_smart_fixtures-2.0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9cdeae2fbbc844c98212f99ea86f45b0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 9057,
"upload_time": "2024-11-08T08:00:50",
"upload_time_iso_8601": "2024-11-08T08:00:50.585504Z",
"url": "https://files.pythonhosted.org/packages/d5/73/1a76b9068659efc824df4a11a5582280fa048347503633fc5bddca10fa35/django_smart_fixtures-2.0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "391522702c590da5fdf182f34b1b516d02eb02432af319cc5b507611b2ce3006",
"md5": "b0a71e112984f92faaf8b7ea145386fa",
"sha256": "09d006ccdac8da2cd5bf39d9c131e0dad37f370ed7a54058cb9452683063e62b"
},
"downloads": -1,
"filename": "django_smart_fixtures-2.0.3.tar.gz",
"has_sig": false,
"md5_digest": "b0a71e112984f92faaf8b7ea145386fa",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 8208,
"upload_time": "2024-11-08T08:00:52",
"upload_time_iso_8601": "2024-11-08T08:00:52.363735Z",
"url": "https://files.pythonhosted.org/packages/39/15/22702c590da5fdf182f34b1b516d02eb02432af319cc5b507611b2ce3006/django_smart_fixtures-2.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-08 08:00:52",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "roknicmilos",
"github_project": "django-smart-fixtures",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "django-smart-fixtures"
}