Skip to content

Instantly share code, notes, and snippets.

@achingachris
Created March 27, 2024 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save achingachris/5cf82dbe3b81f43564d34c23be94562b to your computer and use it in GitHub Desktop.
Save achingachris/5cf82dbe3b81f43564d34c23be94562b to your computer and use it in GitHub Desktop.
from django.core.management.base import BaseCommand
from app.models import Skill, Category
from faker import Faker
import random
class Command(BaseCommand):
help = "Seed database with sample data for app.models"
def add_arguments(self, parser):
parser.add_argument("num_skills", type=int, help="Number of skills to create")
parser.add_argument("num_categories", type=int, help="Number of categories to create")
def handle(self, *args, **kwargs):
num_skills = kwargs["num_skills"]
num_categories = kwargs["num_categories"]
self.stdout.write(self.style.SUCCESS("Seeding database..."))
fake = Faker()
# Create sample skills
existing_skill_titles = set(Skill.objects.values_list("title", flat=True))
for _ in range(num_skills):
skill_title = fake.word()
while skill_title in existing_skill_titles:
skill_title = fake.word()
Skill.objects.create(title=skill_title)
existing_skill_titles.add(skill_title)
# Create sample categories
existing_category_titles = set(Category.objects.values_list("title", flat=True))
for _ in range(num_categories):
category_title = fake.word()
while category_title in existing_category_titles:
category_title = fake.word()
Category.objects.create(title=category_title)
existing_category_titles.add(category_title)
self.stdout.write(self.style.SUCCESS("Database seeding completed."))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment