django_spire/core/management/commands/spire_opencode_pkg/skills/seeding/SKILL.md
Best practices for seeding django models
npx skillsauth add stratusadv/django-spire seedingInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
fields = {
'type': 'faker'
}
fk_random or fk_in_order methods when seedingfields: ClassVar = {
'product_id': ('custom', 'fk_in_order', {'model_class': Product}),
'inventory_id': ('custom', 'fk_random', {'model_class': Invenotry}),
}
custom date_time_between. This follow the same syntax as faker.Different data generation strategies serve different purposes:
("llm", "Describe this product for a sales catalog.")("faker", "word") or ("faker", "date_time_between", {"start_date": "-30d", "end_date": "now"})("static", True) or "in_stock": True("callable", lambda: timezone.now())("custom", "in_order", {"values": supplier_ids})class ProductSeeder(DjangoModelSeeder):
model_class = Product
default_to = "llm" # Options: 'llm', 'faker'
fields = {
'id': 'exclude',
'name': ('llm', 'A product in a clothing sales catalog'),
'description': ('llm', 'Describe this product for a clothing sales catalog.'),
'price': ('faker', 'pydecimal', {'left_digits': 2, 'right_digits': 2, 'positive': True}),
'in_stock': True,
'created_at': ('faker', 'date_time_between', {'start_date': '-30d', 'end_date': 'now'}),
'updated_at': lambda: timezone.now(),
'supplier_id': ('custom', 'in_order', {'values': supplier_ids})
}
Always configure caching for frequently-used seeders:
class ProductSeeder(DjangoModelSeeder):
cache_name = 'product_seeder'
cache_seed = True
default_to = "llm" or "faker" for automatic field populationLeverage built-in custom methods for common patterns:
in_order: Sequential value assignment for maintaining data relationshipsdate_time_between: Random date generation within rangesfk_random: Random foreign key selection from existing recordsfk_in_order: Sequential foreign key assignment for maintaining referential integrityseed() for object instantiation without DB insertionseed_database() for direct database insertionOverride fields to create specific test scenarios:
ProductSeeder.seed(
count=1,
fields={"in_stock": False}
)
Create specialized class methods that need to be re-used:
class ProductSeeder(DjangoModelSeeder):
@classmethod
def seed_grocery_product(cls, count: int = 1):
cls.seed_database(
count=count,
fields=cls.fields | {
'name': ('llm', 'Grocery product name'),
'description': ('llm', 'Grocery product description'),
}
)
# Product Seeder needs to link to batches and inventory
class ProductSeeder(DjangoModelSeeder):
...
@classmethod
def seed_with_inventory(cls, count: int = 1):
products = cls.seed_database(count=count)
for product in products:
BatchSeeder.seed_by_product(
product=product,
count=2,
)
return products
class BatchSeeder(DjangoModelSeeder):
...
@classmethod
def seed_by_product(
cls,
product: Product,
count: int = 1
):
batches = cls.seed_database(
count=count,
fields={
'product_id': product.id
}
)
for batch in batches:
InventorySeeder.seed_database(
count=3,
fields={
'product_id': product.id,
'batch_id': batch.id
}
)
return batches
class InventorySeeder(DjangoModelSeeder):
...
@classmethod
def seed_by_product_and_batch(cls, product, batch, count: int = 1):
"""
Seed inventory items specifically for a given product and batch.
This ensures proper relationship with both product and batch.
"""
return cls.seed_database(
count=count,
fields={
'product_id': product.id,
'batch_id': batch.id
}
)
development
How to implement table templates in Django Spire.
development
How to implement tab templates in Django Spire.
development
Service layer best practices on django models
development
Best practice for working with Django models