- Make sure you have installed
python
on your machine. Download Python pip
package manager is installed. If you have a version ofpython 2 >= 2.7.9 or Python 3 >= 3.4
you will probably havepip
installed if not downloadpip
and in Terminal runpython get-pip.py
. If you have problems pip documentation- This step is not mandatory but recommended is to make a python virtual environment
sudo pip install virtualenv
- Unix based operating systems
- Installation of virtualenv
sudo pip install virtualenv
- Create a directory to hold the project
mkdir ~/projectname
- Create a virtual environment inside the project folder
virtualenv nameofprojectenv
- Installation of virtualenv
- Unix based operating systems
- PostgreSQL is the database in use so make sure you have it installed.
- Unix based operating systems
- updating system variables via Terminal command
sudo apt-get update
- Installing PostgreSQL via Terminal command
sudo apt-get install libpq-dev postgresql postgresql-contrib
updating system variables - Your operating system will make a default user name called
postgres
log in with the commandsudo su - postgres
- Creating a new database
CREATE DATABASE databasename
- Creating a new user
CREATE USER username WITH PASSWORD 'secret'
- To test if you have installed it correctly use command
psql -U username -d databasename
- updating system variables via Terminal command
- Windows operationg systems
- coming soon!
- Unix based operating systems
- Install all requirments run command in the project directory
pip install -r requirements.txt
- Setting up database, add this code to setings.py
DATABASES = { "default": { "ENGINE": "django.db.backends.postgresql", "NAME": "databasename", "USER": "username", "PASSWORD": "secret", "HOST": "localhost", "PORT": "", } }
- Check if the
DEBUG = True
is set in settings.py - Make migrations for database
python manage.py makemigrations
- Migrate models.py to postgres with command
python manage.py migrate
- Run a the test server with command
python manage.py runserver
this will run the server on the default port 8000 if that port is taken you can change the port that the test server runs on withpython manage.py runserver 0.0.0.0:8090
- Import models from framework
from django.db import models
- Import settings from framework
from django.conf import settings
- Import timestampedmodel from database models
from django_extensions.db.models import TimeStampedModel
Example
class Example(models.Model):
city = models.CharField(max_length=50)
street = models.CharField(max_length=45)
postcode = models.CharField(max_length=45)
country = models.CharField(max_length=45)
nr_Of_Floors = models.IntegerField()
building_name = models.CharField(max_length=45)
If you need to implement enum as a field gender = models.CharField(max_length=1, choices=ENUMGENDER)
ENUMGENDER = (
('M', 'Male'),
('F', 'Female'),
)
serializable.py is resopnsible for serializing the data from the orm to json format.
-
Importing serializers
Example
from rest_framework import serializers from .models import ModelClass
-
Creating a new serializable class to json
Note: That these classes work with serializers.HyperlinkedModelSerializer passed as a parameter
- serializing all fields
Example
class ModelClassSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = ModelClass fields = "__all__"
- serializing custom fields
Example
class ModelClassSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = ModelClass fields = ('modelfield', 'modelfield', 'modelfield', 'modelfield')
The REST framework supports automatic urls routing this is a quick way yo make ulrs.
-
Import urls from framework
Example
from django.conf.urls import url, include from django.urls import path
-
Import views from gatherer directory
Example
from . import views
-
Create a Router
- DefaultRouter
Example
router = routers.DefaultRouter()
- SimpleRouter
Example
router = routers.SimpleRouter()
-
Register a Serializer
Example
router.register('url_name', views.ModelClassViewSet)
-
Add router to application urls
Example
urlpatterns = [ path('', include(router.urls)) ]
Creating view sets from the models in the model class.
- Import HttpResponse, render from framework
from django.shortcuts import render, HttpResponse
- Import rest_framework
Example
from rest_framework import generics, viewsets from rest_framework.response import Response from rest_framework import status from rest_framework.reverse import reverse from rest_framework.views import APIView
- Import serializers
from .serializers import ExampleSerializer
- Import models
from .models import Example
class ExampleViewSet(viewsets.ModelViewSet):
queryset = Example.objects.all()
serializer_class = ExampleSerializer
-
Customizing a admin model
- Custom class Example sdadasdasd
class ModelClassAdmin(admin.ModelAdmin): list_display = ('modelfield', 'modelfield') model = models.ModelClass
- Register a custom class
Example
admin.site.register(models.ModelClass, ModelClassAdmin)
- Register with framework default
Example
admin.site.register(models.ModelClass)
- Martin Savov - Initial work - MIT120
- Hristiyan Tarnev - Initial work - Beasteca
- Ralitsa Kehayova - Initial work - Shortcut21
- Artjom Leonov - Initial work - qniff
See also the list of contributors who participated in this project.
Copyright (c) <2018>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.