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
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 18.5MB
glibcpy 3.10–3.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]}")
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.