Tutorial to make a Python Django API with GraphQL
- Django hello world: https://docs.djangoproject.com/en/2.1/intro/tutorial01/
- Django + graphene hello world: https://docs.graphene-python.org/projects/django/en/latest/tutorial-plain/
- Django youtube tutorial with sentdex https://www.youtube.com/watch?v=FNQxxpM1yOs
- https://www.python.org/downloads/
- Direct: https://www.python.org/ftp/python/3.7.1/python-3.7.1-amd64.exe
- https://www.python.org/downloads/mac-osx/
- https://www.python.org/ftp/python/3.7.1/python-3.7.1-macosx10.9.pkg
apt install python3.7
If packages are not in main repositories, install from testing repositories
sudo echo "deb http://ftp.de.debian.org/debian testing main" >> /etc/apt/sources.list
echo 'APT::Default-Release "stable";' | sudo tee -a /etc/apt/apt.conf.d/00local
sudo apt-get update
sudo apt-get -t testing install python3.7
python3.7 -V
sudo pip install virtualenv
sudo pip install virtualenvwrapper
export PYTHONUSERBASE=$HOME
pip install --user virtualenv
pip install --user virtualenvwrapper
- https://virtualenv.pypa.io/en/latest/installation/
- https://virtualenvwrapper.readthedocs.io/en/latest/install.html
Create and start the virtual environment
virtualenv /path/to/project
source /path/to/project/bin/activate
To stop the the virtual environment, type deactivate
pip install django
Create a project called main. This is the root project: blog, user etc will be smaller apps within this project
django-admin startproject main
Go to the newly created project folder and run the server
cd main
python3.7 manage.py runserver
For another port than the default, add the IP and port at the end, i.e.
python3.7 manage.py runserver 0:4000
(0 is short for 0.0.0.0)
Navigate to http://localhost:8000 with your browser and see Django welcome page
Terminate server with CTRL-C
Go to the the project directory main to create the first module, an app called blog
cd main # if this is not already your current working directory
django-admin startapp blog
Navigate to the tutorials and follow through from https://docs.djangoproject.com/en/2.1/intro/tutorial03/ - Write your first view to https://docs.djangoproject.com/en/2.1/intro/tutorial03/ - Write views that actually do something
Tweak your models to your app. For example, a blog might have a model set up looking something like this
Optional.
# Empty database
python3 manage.py sqlflush # delete all rows from all database tables
# Remove migrations
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete # remove the current migration files
rm db.sqlite3 # remove the database
# Create new migration
python3 manage.py makemigrations # create the migration files inside migrations/ folder
python3 manage.py migrate # migrate the tables to the database
# Show migrations
python3 manage.py sqlmigrate blog 0001 # show the SQL generated b first migration in app called "blog"
python3 manage.py showmigrations # show which files are migrated in each app (django default apps and your apps)
# Admin user
python3 manage.py createsuperuser # creates the superuser to login with on http://localhost:8000/admin
# Run server
python3 manage.py runserver # run the server
If you're lazy, create an easy alias for python3 manage.py
, at the bottom of your ~/.bashrc file:
alias pym='python3 manage.py'
Note: You must deactivate your current environment, open a new shell and activate it again for the alias to take effect
Create a file called ~/python-reset-migrations.sh and paste the following:
# Remove migration files
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
# Remove db
rm db.sqlite3
# Create the migration files
python3 manage.py makemigrations
# Migrate
python3 manage.py migrate
# Create superuser
python3 manage.py createsuperuser --username halpdesk --email [email protected] --no-input
python3 manage.py changepassword halpdesk
Note: Change the username "halpdesk" to your preferred username in the above script
Note: You must deactivate your current environment, open a new shell and activate it again for the alias to take effect
Add the following alias in you ~/.bashrc file:
alias migrateall='/bin/bash ~/python-reset-migrations.sh'
Main documentation: https://github.com/graphql-python/graphene-django
Go to the root of your project and install graphene for django. Remember to activate your virtual environment if you do not have it activated.
cd /path/to/proj
source bin/activate
pip install "graphene-django>=2.0"
Note: you can use
pip list
to check the current list of installed packages