21 lines
815 B
Python
21 lines
815 B
Python
from django.core.management.base import BaseCommand
|
|
from ...models import AgeGroups, Event
|
|
import random
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Populate the AgeGroup model with predefined age groups'
|
|
|
|
def handle(self, *args, **kwargs):
|
|
age_groups = ["18-21", "21-30", "30-40", "40-50", "50+"]
|
|
for age in age_groups:
|
|
age_group, created = AgeGroups.objects.get_or_create(name=age)
|
|
if created:
|
|
self.stdout.write(self.style.SUCCESS(f'Age group "{age}" created.'))
|
|
else:
|
|
self.stdout.write(self.style.WARNING(f'Age group "{age}" already exists.'))
|
|
|
|
# Update all Event objects with a random age group
|
|
for event in Event.objects.all():
|
|
event.age_group = random.choice(age_groups)
|
|
event.save()
|