35 lines
1.0 KiB
Python
35 lines
1.0 KiB
Python
"""Command to create test data fixture for teams."""
|
|
|
|
import json
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from mainapp.models import Team
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = "Creates a fixture file for Team objects"
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument("amount_of_teams", type=int, help="Number of teams to create")
|
|
|
|
def handle(self, *args, **options):
|
|
teams_amount = options["amount_of_teams"]
|
|
|
|
# Create the new teams (this will create them in the database)#
|
|
new_teams = [Team.objects.create() for i in range(teams_amount)]
|
|
|
|
teams_fixture = [{
|
|
"model": "mainapp.team",
|
|
"pk": team.pk,
|
|
"fields": {}
|
|
} for team in new_teams]
|
|
|
|
# Remove the teams from the database
|
|
for team in new_teams:
|
|
team.delete()
|
|
|
|
with open("src/mainapp/fixtures/teams_fixture.json", "w") as file:
|
|
file.write(json.dumps(teams_fixture, indent=4))
|
|
self.stdout.write(self.style.SUCCESS("Created teams_fixture.json."))
|