Skip to content

Instantly share code, notes, and snippets.

@iscott
Last active January 22, 2021 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save iscott/b804991fa36271c725477e34c7cea01f to your computer and use it in GitHub Desktop.
Save iscott/b804991fa36271c725477e34c7cea01f to your computer and use it in GitHub Desktop.

Run Server:

python3 manage.py runserver

Create DB:

createdb

Make migrations:

python3 manage.py makemigrations

Migrate DB:

python3 manage.py migrate

Show Migrations:

python manage.py showmigrations

Create a project:

django-admin startproject

Create an app:

python3 manage.py startapp main_app

Create an admin user for the Django Admin site:

python3 manage.py createsuperuser

Run Django Shell:

python3 manage.py shell

Import models in shell:

from <app_name>.models import *

Shell commands:

c = Cat.objects.get(id=3) # return one
Cat.objects.filter(id=3) # return QuerySet
Question.objects.filter(question_text__startswith='What')
Cat.objects.all()
Cat.objects.first()

Create a record:

q = Question(question_text="What's new?", pub_date=timezone.now())
q.save()

To access through a has_many relationship, append _set. Example:

# display all Choices (from Choice model) related to Question
Question.objects.last().choice_set.all()
# How many questions are in the questions table?
Question.objects.all().count()

delete a record:

c.delete()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment