Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# python manage.py shell
from polls.models import Poll, Choice
p = Poll.objects.all()[0]
import datettime
import random
def pick_random(choices):
rand_index = random.randint(0, len(choices) - 1)
return str(choices[rand_index])
def pad_zero_if_needed(string):
return int(string) < 10 and '0{0}'.format(string) or string
def generate_date():
year = pick_random(range(2007, 2014))
month = pad_zero_if_needed(pick_random(range(1, 12)))
day = pad_zero_if_needed(pick_random(range(1, 31)))
hour = pad_zero_if_needed(pick_random(range(0, 23)))
minute = pad_zero_if_needed(pick_random(range(0, 59)))
date_str = '-'.join([year, month, day]) + ' ' + ':'.join([hour, minute]) + ' UTC'
try:
date = datetime.datetime.strptime(date_str, "%Y-%m-%d %H:%M %Z")
except ValueError, e:
date = datetime.datetime.now()
finally:
return date
for i in range(1, 201):
question = 'Question {0}'.format(i)
date = generate_date()
Poll.objects.create(question=question, pub_date=date)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.