Working Ninja
2016-02-04T08:04:04
class AbstractClass(models.Model):
    common_attribute = models.CharField()

    class Meta:
        abstract = True


class SpecializedClass(models.Model):
    specialized_attribute = models.CharField()

    class Meta:
        abstract = True


class AnotherSpecializedClass(models.Model):
    another_specialized_attribute = models.CharField()

    class Meta:
        abstract = True


class MultipleInheritance(AbstractClass, SpecializedClass):
    pass


class TripleMulitpleInheritance(AbstractClass, SpecializedClass, AnotherSpecializedClass):
    pass

Sources: https://docs.djangoproject.com/en/1.9/topics/db/models/#abstract-base-classes,
https://docs.djangoproject.com/en/1.9/topics/db/models/#multiple-inheritance

2016-01-11T22:52:32

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

$ rm -rf db.sqlite3
$ rm -rf app/migrations/*
$ ./manage.py makemigrations --empty app
$ ./manage.py makemigrations
$ ./manage.py migrate
$ ./manage.py createsuperuser
2016-01-11T21:47:37

Print all digits in a string as one integer:

>>> string = "a1b2c3"
>>> integer = int(filter(str.isdigit, string))
>>> print integer
123
>>> 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'>
2015-12-05T17:32:59

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!

2015-11-27T11:40:34

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

STATICFILES_DIRS = (
    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' %}.

Source: https://docs.djangoproject.com/en/1.8/howto/static-files/

2015-10-14T10:22:24
SELECT count(email),email FROM auth_user GROUP BY email HAVING count(email) > 1;