35312-vm/core/management/commands/populate_db.py
Flatlogic Bot 9ce0135bc0 v1
2025-10-29 00:35:09 +00:00

39 lines
1.4 KiB
Python

from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from core.models import Category, Article
class Command(BaseCommand):
help = 'Populates the database with sample data'
def handle(self, *args, **options):
self.stdout.write('Deleting existing data...')
Article.objects.all().delete()
Category.objects.all().delete()
User.objects.filter(is_superuser=False).delete()
self.stdout.write('Creating new data...')
# Create users
editor_user = User.objects.create_user(username='editor', password='password')
# Create categories
general_cat = Category.objects.create(name='General', slug='general')
tech_cat = Category.objects.create(name='Technical', slug='technical')
# Create articles
Article.objects.create(
title='Getting Started with Django',
content='This is a beginner-friendly guide to start your journey with the Django framework.',
category=tech_cat,
author=editor_user
)
Article.objects.create(
title='Our Company Policy',
content='An overview of our company policies and guidelines for all employees.',
category=general_cat,
author=editor_user
)
self.stdout.write(self.style.SUCCESS('Successfully populated the database.'))