A Django Rest App With Type Annotated Way
There is a lot of Django CRUD tutorial out there. I was looking for a tutorial which uses Python type annotation but there is no one which I like, so I learn and made one. The benefit of type annotation is its give you a type integrity violation warning if you put any mismatching type there. The benefit is its help you find bug earlier though there is no performance gain or loss. There is a github link i put there, you can clone it and run the project.
InterconnectionProject - Interconnection Simple Task Projectgithub.com
First you need to install Python3.5+ version it will also install Python package management tool pip, there is other tool called `pipenv` available which is like combination of pip and virtual environment, its make managing project breeze. Lets try it, first install it using pip
pip install pipenv
It will install pipenv, we will use it managing and sandboxing our python project. It will make interference free from other python project on same host machine. Now we will install Python and Django and start making our project
pipenv --three # Creating a virtualenv for this project…
pipenv install django # Adding django to Pipfile’s [packages]…
To activate this project’s virtualenv, run the following:
After running this command, it will bring us in project virtual environment then run following command
django-admin startproject InterconnectionContact .
It will create InterconnectionContact project in your current directory. Don’t forget to put dot `.` after project name otherwise it will create same named parent directory and project name which will create confusion. Now check directory structure. It will look something like this
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
Now check either your project working properly
pipenv run ./manage.py runserver
Performing system checks…
System check identified no issues (0 silenced).
You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run ‘python manage.py migrate’ to apply them.
April 02, 2018–11:30:44
Django version 2.0.3, using settings ‘InterconnectionContact.settings’
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
If Things goes right way you will see something like this.
Let create an app for this project. Run the following command
pipenv run ./manage.py startapp contact
It will create app contact for project ‘InterconnectionContact’. Let check the directory structure.
Thats our initial project structure. Let write some code. We will actually write code in views.py and models.py file and create other file urls.py in contact directory which will get incoming request from project main urls.py file. Let check code in models.py file
Now migrate project which will create table in database, for simplicity we will use builtin sqlite3 database.
python3.6 manage.py migrate
Lets create a views.py file, we will write all business logic there.
In this views.py file we apply all CRUD operation using two urls. When user send any request through urls.py file, it will go find and execute proper method in class in views.py file. Here is our urls.py file
For model serialization deserialization there is a file serializers.py. It is used for converting Python object to json or vice versa.
And finally there is a root url.py file in InterconnectionProject/urls.py. All incoming request first come here and reroute it to app contact/urls.py file.
Thats all we have for you right now. If you want to run this project clone the project from github repo and follow the instruction given in README.md file.