2015-12-05T17:32:59
Simple Scripting in Django
I needed to create a slew of objects for a project I was working on. Lots of tedious and error prone work. But wait, I'm using a computer. Automation to the rescue!
I wrote my script by first importing what I needed from Django (User and the Appointment model for the objects I was going to be creating). Then, I wrote what needed to be created enclosed in a for loop. To run the script, I jumped into Django's shell and ran the execfile()
command.
from django.contrib.auth.models import User
from app.models import Appointment
user_ids = [7, 8, 9, 10, 11, 12]
for i, user_id in enumerate(user_ids, start=1):
Appointment.objects.create(user=User.objects.get(id=user_id),
title="Test appointment.",
date="2015-12-01",
timeslot=i,
doctornotes="Sample doctors notes.")
./manage.py shell
>>> execfile('app/scripts/create_appointments.py')
Profit!