def create_patient(firstname, middlename, lastname, sex): try: usr = User.objects.get(first_name=firstname, last_name=lastname) return Patient.objects.get(user=usr) except Exception, e: firstclean = firstname.replace("'","") lastclean = lastname.replace("'","") dob = date(random.randint(1955,2008), random.randint(1,12), random.randint(1,28)) pt = Patient() pt.sex = sex pt.dob = dob pt.user = create_user(username=firstclean.lower() + "_" + lastclean.lower(), password='******', firstname=firstname, lastname=lastname) pt.is_primary = True pt.save() transaction.commit() return pt