Skip to content

Instantly share code, notes, and snippets.

@fodra
Last active March 26, 2017 22:39
Show Gist options
  • Save fodra/f33984b3dee8141c9aaa42ec71afe78a to your computer and use it in GitHub Desktop.
Save fodra/f33984b3dee8141c9aaa42ec71afe78a to your computer and use it in GitHub Desktop.
A bunch of usually used django commands.

Create

Start a new project

$ django-admin startproject moneymakingsite

Create a new django application

$ python manage.py startapp merchandise

What's the difference between a project vs. application?

REST

Show URLs

Comes in handy if the front-end developer needs a list of available APIs

$ python manage.py show_urls

/api/user/	apps.users.rest_views.UserListView	api:user-list
/api/user/<pk>/	apps.users.rest_views.UserObjectView	api:user-object
/api/user/avatar/	apps.users.rest_views.upload_avatar	api:upload-avatar

Debugging

Django Shell

If you want to quickly test some stuff, specially with the ORM. You could run the django shell.

$ python manage.py shell

Python 3.6.0 (default, Dec 24 2016, 08:01:42)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from apps.events.models import Threshold
>>> default = Threshold.default()
>>> Threshold.objects.count()
3

Database Migrations

Create an empty migration file

$ python manage.py makemigrations --empty yourappname --name migrationname

Show all migrations that were applied

$ python manage.py showmigrations

users
 [X] 0001_initial
 [X] 0002_auto_20150814_1620
 [X] 0003_auto_20151119_1423
 [X] 0004_auto_20151123_1640
 [X] 0005_auto_20151125_1307
 [X] 0006_myuser_thresholds
 [X] 0007_auto_20160127_1025
 [X] 0008_auto_20160512_1812
 [X] 0009_auto_20160512_1813
 [X] 0010_measure_unit
 [X] 0011_sitefeedback
 [X] 0012_auto_20170110_0940
 [X] 0013_auto_20170131_2018
 [X] 0014_remove_myuser_is_superuser
 [X] 0013_myuser_created_at
 [X] 0015_merge_20170303_0932
 [X] 0016_auto_20170308_1159
 [X] 0017_auto_20170310_1109
 [X] 0018_auto_20170316_1026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment