Last active
December 13, 2018 06:22
-
-
Save sitebuilderone/0236a72c22716b836cf203224f752928 to your computer and use it in GitHub Desktop.
Simple Django commands for reference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
django_commands.md | |
#django version - on MAC using version 3 | |
python3 -m django --version | |
#Create a project | |
django-admin.py startproject antonblog | |
# run project | |
python3 manage.py runserver | |
# check on server | |
http://127.0.0.1:8000/ | |
APPS | |
# create new app | |
python3 manage.py startapp my_custom_app | |
ADMIN | |
# detects changes | |
python3 manage.py makemigrations | |
# apply | |
python3 manage.py migrate | |
#create an admin user | |
python3 manage.py createsuperuser | |
DATABASE - ORM Object Relationial Mappers | |
Sample | |
from django.db import models | |
from django.utils import timezone | |
from django.contrib.auth.models import User | |
class Post(models.Model): | |
title = models.CharField(max_length=100) | |
content = models.TextField() | |
#date_posted = models.DateTimeField(auto_now=True) | |
date_posted = models.DateTimeField(default=timezone.now) | |
author = models.ForeignKey(User, on_delete=models.CASCADE) | |
# make the migration | |
python3 manage.py makemigrations | |
# show sql code | |
python3 manage.py sqlmigrate blog 0001 | |
# Django Python Shell | |
# run shell command | |
python3 manage.py shell | |
# import post & user model | |
# show all users | |
from blog.models import Post | |
from django.contrib.auth.models import User | |
User.objects.all() | |
from blog.models import Post | |
from django.contrib.auth.models import User | |
User.objects.first() | |
#filter by user | |
from blog.models import Post | |
from django.contrib.auth.models import User | |
User.objects.filter(username='anton7').first() | |
user = User.objects.filter(username='anton7').first() | |
# command | |
user | |
user.id | |
user.pk # primary key | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment