Don't be stupid, and load fixtures in a smart way
Project description
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
pip install django-smart-fixtures
Configuration
Add smart_fixtures to your INSTALLED_APPS:
INSTALLED_APPS = [
...
'smart_fixtures',
...
]
FIXTURES settings configuration
Let's say you have the following fixtures:
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:
# 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:
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:
- 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.jpgmy_app/images/image2.jpgmy_other_app/files/file1.txtmy_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.
# 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:
# 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:
-
Update the version in the
pyproject.tomlfile. -
Build the package:
poetry build -
Configure your PyPI credentials (if you haven't already):
poetry config pypi-token.pypi <your-pypi-token>
-
Publish the package:
poetry publish
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request or open an Issue.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_smart_fixtures-2.0.4.tar.gz.
File metadata
- Download URL: django_smart_fixtures-2.0.4.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.3 Linux/6.8.0-57-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5771497b51b6681006b0ee122f61fcd25634c0a78b9343aca3d0d620abc971bf
|
|
| MD5 |
fce4e80ea0f01d23e057072d5ef6b4a5
|
|
| BLAKE2b-256 |
e74bc27e6c43b39b593af14cb79a539b4cb8799e2288c90662b360ab53f08326
|
File details
Details for the file django_smart_fixtures-2.0.4-py3-none-any.whl.
File metadata
- Download URL: django_smart_fixtures-2.0.4-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.3 Linux/6.8.0-57-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ae747b9afac7e6872ca1ca864a2bb6e8f3d0d51ec140753302c6c05efa15db4f
|
|
| MD5 |
547d59fc000e0fd21a52b0facadb1fca
|
|
| BLAKE2b-256 |
7e84e0cb693142cf244a583ceaf5cf32185aa2402d34337d9dbbd59f81d3f961
|