Working Ninja

Note: the following commands drop all data from the database.

$ rm -rf db.sqlite3
$ rm -rf app/migrations/*
$ ./ makemigrations --empty app
$ ./ makemigrations
$ ./ migrate
$ ./ createsuperuser

Print all digits in a string as one integer:

>>> string = "a1b2c3"
>>> integer = int(filter(str.isdigit, string))
>>> print integer
>>> type(integer)
<type 'int'>

Or, if you would like a list of all digits:

>>> string = "a1b2c3"
>>> import re
>>> integer_list = re.findall('\d+', string)
>>> print integer_list
['1', '2', '3']
>>> type(integer_list)
<type 'list'>

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):
                               title="Test appointment.",
                               doctornotes="Sample doctors notes.")
./ shell

>>> execfile('app/scripts/')



Static files for an app follow the following directory structure my_app/static/my_app and are picked up by ./ collectstatic when ready to deploy. If you're wanting to use static files that span multiple apps, update your to include the following:

    os.path.join(BASE_DIR, "project_static"),

Temping it as it is, STATICFILES_DIRS cannot contain the location of STATIC_ROOT. Manually placing your static files where the collectstatic management command dumps its static files does not work nor does setting "project wide" static files into the same directory work, thus the separate "project_static" directory. Dropping files in here will allow them to be accessable via {% static 'file' %}.


SELECT count(email),email FROM auth_user GROUP BY email HAVING count(email) > 1;