I've been coding in Django for a while now and I found myself having to do certain things repetitively. So I looked for an easier way.
Imagine you have to randomize data in a certain field but you have 1000 records 😄 yeah 1000 records, and you have to update every single field because you don't want them to be the same.
Or you need to populate your DB with 100 records, you don't want to type all of that manually.
That's where Django management commands come in.
What are management commands?
They're custom scripts you can run from the terminal using python manage.py your_command. Django already ships with built-in ones like migrate, createsuperuser, and collectstatic — you're just creating your own.
You've probably run those a hundred times without thinking about it. Same idea, but now you define what happens.
How to create one
Inside any app, create this folder structure:
your_app/
management/
__init__.py
commands/
__init__.py
seed_posts.py ← your command lives here
Then inside seed_posts.py:
from django.core.management.base import BaseCommand
from faker import Faker
from posts.models import Post
from accounts.models import User
fake = Faker()
class Command(BaseCommand):
help = 'Seed the database with dummy posts'
def handle(self, *args, **kwargs):
users = User.objects.all()
for _ in range(100):
Post.objects.create(
author=fake.random_element(users),
title=fake.sentence(),
body=fake.paragraph(nb_sentences=10),
)
self.stdout.write(self.style.SUCCESS('✅ 100 posts created successfully'))
Run it with
python manage.py seed_posts
Done. 100 posts in seconds.
Why this matters
- No more manually clicking through the admin to create test data
- You can reset and reseed your DB anytime during development
- Makes onboarding other devs easier — they clone the repo, run the seed command, and they're ready to go
Management commands are one of those Django features that feel small until you actually need them — then you wonder how you ever lived without them.
What do you use management commands for? Drop it in the comments 👇
Loading thoughts...