>> python manage.py check
System check identified no issues (0 silenced).
>> python manage.py makemigrations xxx # xxx is apps name
Migrations for 'verity':
verity/migrations/0001_initial.py
- Create model CaseStatus
- Create model RefereesDetails
- Create model RefereesMain
- Add field main to refereesdetails
- Add field details to casestatus
>> python manage.py migrate xxx 0001
Operations to perform:
Apply all migrations: verity
Running migrations:
Applying verity.0001_initial... OK
SQL management
python manage.py sqlmigrate myapp 0001_initial
Admin Dashboard
# setup the super user to login the admin page
python manage.py createsuperuser <user_name>
# setup password for super user to login the admin page
python manage.py changepassword <user_name>
# update the admin.py
from django.contrib import admin
# Register your models here.
from stocksapi.models import Visitor
class VisitorAdmin(admin.ModelAdmin):
list_display = ('date_time','latency')
heroku Setup
create a working dir with virtual
virtualenv herokuenv
cd herokuenv
source bin/activate
heroku base on the defined python version in runtime.txt to setup the environment, create runtime.txt inside monitorapi_heroku folder
python-3.6.5
It betters to create another setting file for heroku, as the environment on heroku is different with local development machine, create prod_settings.py in monitorapi folder which contains settings.py
from .settingsimport*STATIC_ROOT='staticfiles'SECURE_PROXY_SSL_HEADER= ('HTTP_X_FORWARDED_PROTO', 'https')
ALLOWED_HOSTS= ['monitorapi.herokuapp.com','*']
DEBUG=False
Create .gitignore file to save the space on heroku, we don't want to upload files like *.pyc to heroku
Install pgadmin tools (pgAdmin 4) to manage database via UI
Local setup
# setup the url link
export DATABASE_URL=postgres://$(whoami)
# pull the database form heroku to local
heroku pg:pull DATABASE_URL herokulocaldb --app monitorapi
# push the database form local to herku
heroku pg:push herokulocaldb DATABASE_URL --app monitorapi
# example:
▸ Remote database is not empty. Please create a new database or use heroku pg:reset
Scitys-iMac:monitorapi_heroku Scity$ heroku pg:reset DATABASE
› Warning: heroku update available from 7.21.0 to 7.27.1
▸ WARNING: Destructive action
▸ postgresql-rugged-43376 will lose all of its data
▸
▸ To proceed, type monitorapi or re-run this command with --confirm monitorapi
> monitorapi
How to update the model on heroku
Updated the modeles.py, e.g. updated fields or add new table
use python manage.py makemigrations to generate the file (stocksapi/migrations/0001_initial.py) under migrations
Check in the file
git add stocksapi/migrations/0001_initial.py
git commit -m Added "new migration file"
git push heroku master
Let heroku create the table remotely on remote Heroku Postgres
heroku run python manage.py migrate
Postgres Setting in Django
to connect to a postgreSQL server with python, it needs to install the psycopg2 library:
pip install psyconpg2
# update the database setting in settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'testdb',
'USER': 'Scity',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432'
}
}
# create all of the djangop tables (around 10 tables) on new database
python manage.py migrate
Fixed the admin page broken issue on Heroku
deploying to Heroku without using whitenoise (which I would suggest), definitely use dj_static