Registry / testing / django-dynamic-fixture

django-dynamic-fixture

JSON →
library4.0.1pypypiunverified

Django Dynamic Fixture (DDF) is a full library designed to create dynamic model instances primarily for testing purposes. It allows developers to generate complex Django model objects with sensible default data, reducing boilerplate in tests. As of version 4.0.1, it supports modern Django and Python versions. Releases typically follow major Django version compatibility updates, with minor bug fix releases in between.

pip install django-dynamic-fixture
INSTALL
IMPORT
SIG · DJANGO-DYNAMIC-FIX
D
django-dynamic-fixture
testingpythonv4.0.1
Install
1.6s avg
Import
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.0.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 18.5MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.6s · import 0.000s · 19MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

G
from ddf import G
from django_dynamic_fixture import G

This quickstart demonstrates how to use `ddf.G` to create and persist model instances, `ddf.F` for dynamic field values (including using `Faker`), and `ddf.N` for non-persisted instances. It includes a minimal, self-contained Django setup to make the code runnable without a pre-existing Django project.

import os from django.conf import settings from django.apps import apps from django.db import models from ddf import G, F, N from faker import Faker # Minimal Django setup for the quickstart to be runnable without a full project if not apps.ready: settings.configure( DEBUG=True, INSTALLED_APPS=[ 'django.contrib.auth', 'django.contrib.contenttypes', ], DATABASES={ 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:', } }, SECRET_KEY='not-a-secret', USE_TZ=True, # Define a dummy app_label for models in this ad-hoc setup MIGRATION_MODULES={'myapp': None} # Prevent migrations check ) import django django.setup() # Define dummy models for the quickstart within a custom app_label class Author(models.Model): name = models.CharField(max_length=255) email = models.EmailField(unique=True) bio = models.TextField(blank=True) class Meta: app_label = 'myapp' # Required for models in minimal setup def __str__(self): return self.name class Book(models.Model): title = models.CharField(max_length=255) author = models.ForeignKey(Author, on_delete=models.CASCADE) pages = models.IntegerField(default=100) class Meta: app_label = 'myapp' # Required def __str__(): return self.title # --- Actual django-dynamic-fixture usage --- fake = Faker() print("--- Creating instances ---") # 1. Create a single Author instance author1 = G(Author, name=fake.name(), email=fake.email(), bio=fake.paragraph()) print(f" Created Author: {author1.name} ({author1.email})") # 2. Create a Book instance referencing an existing author book1 = G(Book, author=author1, title=fake.sentence(nb_words=4)) print(f" Created Book: '{book1.title}' by {book1.author.name}") # 3. Create multiple Books with dynamic values using F() # This will also create a new Author because 'author__name' is specified books_dynamic = G(Book, n=2, author__name=F(fake.name), author__email=F(fake.email), title=F(lambda: fake.sentence(nb_words=3)), pages=F(lambda: fake.random_int(min=50, max=500))) for book_obj in books_dynamic: print(f" Created Dynamic Book: '{book_obj.title}' by {book_obj.author.name} ({book_obj.pages} pages)") # 4. Create a non-persisted instance (not saved to database) draft_book = N(Book, title="Draft Idea", author=author1, pages=20) print(f" Created Draft Book (not persisted): '{draft_book.title}' by {draft_book.author.name}") # You can verify it's not saved (requires django.db.models import and Django setup) # from django.db import connection # with connection.cursor() as cursor: # cursor.execute("SELECT COUNT(*) FROM myapp_book") # print(f"Total books in DB: {cursor.fetchone()[0]}")
Debug
Known issues
breakingThe `Fixture` class was renamed to `F` in version 4.0.0. Code using `from ddf import Fixture` or `ddf.Fixture(...)` will break.
fix
Update imports from `from ddf import Fixture` to `from ddf import F` and replace all `Fixture(...)` calls with `F(...)`.
affects: 4.0.0+
breakingVersion 4.0.0 dropped support for older Python and Django versions. Specifically, it requires Python >= 3.8 and Django >= 3.2.
fix
Ensure your project runs on Python 3.8+ and Django 3.2+ before upgrading to django-dynamic-fixture v4.x. If not possible, stick to django-dynamic-fixture v3.x.
affects: 4.0.0+
gotchaWhen using `ddf.G` for related fields (ForeignKey, ManyToManyField, OneToOneField), passing `F(...)` directly for the related object (e.g., `author=F(...)`) will not create the related object automatically. Instead, define nested attributes using double underscores (e.g., `author__name='...'`) or pass an existing object.
fix
To create a related object and link it, use the `related_field__attribute=value` syntax (e.g., `G(Book, author__name='New Author')`). To link to an existing object, pass the object directly (e.g., `G(Book, author=my_author_instance)`).
affects: All versions
Upgrade
Version history
4.0.1latest on PyPI · released Sep 15, 2023
Audit
Dependencies
DjangorequiredCore dependency for a Django library to function. DDF v4.x requires Django >= 3.2.
FakeroptionalProvides more realistic and varied data for generated fields. Highly recommended for rich test data.
Agent activity
10 hits · last 30 days
node
10
Resources
django-dynamic-fixture — pip install django-dynamic-fixture · libregistry