Skip to main content
This python module was written to help create database fixtures from an existing
database that is too large to simply dump it in its entirety.
Thanks to Jacob Rigby, Technical lead at Reya Group (http://www.reya.com)
for his unhesitating agreement to release this to the open source community.

The process copies the desired model instances to a separate database
along with their dependencies, and optionally children (instances dependent on
them). You can then dump the other database to get a minimal fixture
for your test case.

Quick Reference:
def db_sample(
db_obj_iterable, # Iterable containing Django model instances
child_depth=1, # Set this to 2+ to also include children of children
db_alias='fixture_maker' # If you really need to use a different alias you can.
)

First use:
1. Add a database 'fixture_maker' to your settings file pointing to a
database with a different name from your default database. Add
"'TEST_MIRROR': 'default'" to the fixture_maker database settings
to prevent the new database from being created during tests.

2. Create the database pointed to by alias fixture_maker (This step can be skipped
if you're using sqlite, as it will automatically create the database when
sync'd)

3. Run 'python manage.py syncdb --database=fixture_maker'.
*note I've run into some trouble with South, syncdb, and the --database
flag. If you're using south, you can work around this:
Move your default database, do a 'syncdb --migrate' then rename
the resulting new default database as
the fixture maker database then move your original default database back.

4. Pass an iterable containing django model instances to db_sample().
This Example will save all the Pizza instances with olive topping to the
fixture_maker database with their dependencies and by default children to
1 level deep:

from db_sampler_script import db_sample

p = Pizza.objects.filter(toppings__type='olive')
db_sample(p)
5. Run 'python manage.py dumpdata --database=fixture_maker > your_new_fixture'

Enjoy.

Subsequent use:
Skip steps 1 & 2, and replace step 3 with:
'python manage.py flush --database=fixture_maker'
Be careful not to run this command without the --database flag, lest
you erase your entire default database.

Assumptions:
Primary key for models is obj.id, if you've changed this for some models,
please let me know how it blows up.
Foreign keys don't form loops (an object reference an object which eventually
references the first). This is very rarely needed, and many databases
make it difficult, so if you're not sure don't worry about it.
If you have some loops like these and genuinely need them, let me know &
I'll update the code to deal with it.
Foreign keys that aren't NULL are assumed to be required for database
consistency. At worst this will add some model instances to the resulting
fixture that aren't really needed. Unless someone contacts me about
this causing a real problem, it will likely stay like this.

Metadata

Release files for django_db_sampler 0.21

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distributions (sdists)

Source distribution for django_db_sampler 0.21
File Size Uploaded Provenance Actions
django_db_sampler-0.21.zip 5.4 kB Without provenance Details for django_db_sampler-0.21.zip
django_db_sampler-0.21.tar.gz 4.4 kB Without provenance Details for django_db_sampler-0.21.tar.gz

Release files / django_db_sampler-0.21.zip

Download URL django_db_sampler-0.21.zip
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
80e84ede37b4f83f3972bb8f3b3d410ff3a52bc474f7a1e00468eb04aa0b2688
BLAKE2b-256 checksum
How to use checksums
a7b5ea8342856e8319e871aee4fc5639d7b672c6cb275c59e25abff6b676d48e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_db_sampler-0.21.tar.gz

Download URL django_db_sampler-0.21.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
6f514bad5cb7ba571936b1ae85c86fece177968a4ddab53f0b99b28dd72fb069
BLAKE2b-256 checksum
How to use checksums
cc658b77125c8c605410e8a48d51f26c5a254ba4d4198cca9303f5cd6dbcb457
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for django_db_sampler 0.21

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.21 This release

2 release files No provenance

0.2

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page