Added add_students script to add sample students

This commit is contained in:
Challenger 2023-06-08 23:58:11 +05:30
parent fb7d0550fe
commit 609eb1a159
1 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,59 @@
import random
from django.db.utils import IntegrityError
from APIs.models import Student
# To run this script run the following command:
# python manage.py runscript add_students --script-args <add_type> <num_of_stundents_to_add>
def run(*args):
# Throw error if invalid number of arguments passed
if not args:
raise ValueError("Invalid number of arguments passed")
if args[0] in ("manual", "man") :
if not args:
raise ValueError("Invalid number of arguments passed")
for i in range(1, int(args[1])+1):
student = Student.objects.create(
id = input("Enter id: "),
roll_no = input("Enter roll_no: "),
name = input("Enter name: "),
branch = input("Enter branch: "),
phone_number = input("Enter phone_number: "),
cpi = input("Enter cpi: "),
degree = input("Enter degree: "),
batch = input("Enter batch: ")
)
student.save()
elif args[0] in ("auto", "automatic"):
if not args:
raise ValueError("Invalid number of arguments passed")
for i in range(1, int(args[1])+1):
try:
student = Student.objects.create(
id = i-1,
roll_no = 220010000 + i,
name = "Student " + str(i),
branch = random.choice(["CSE", "EE", "MECH", "CHEM", "CIVIL", "EP", "BSMS", "MNC"]),
phone_number = random.randint(1000000000, 9999999999),
cpi = random.random()*10,
degree = "bTech",
batch = random.randint(2018, 2023)
)
student.save()
except IntegrityError:
pass
elif args[0] in ("del", "delete"):
# delete students with name starting with Student
s = Student.objects.filter(name__startswith="Student")
s.delete()
else:
raise ValueError("Invalid argument passed")