This file contains 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
--Virtual Environment | |
$sudo apt install python3-venv python3-pip | |
$python3 -m venv “/path to your location folder” | |
--activate the virtual environment | |
$ source <location folder>/bin/activate | |
--install latest django version | |
$pip install django | |
$pip install djangorestframework | |
---start django project and app. | |
$django-admin startproject backend |
This file contains 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
--ساخت دیتابیس در شاخه root پروژه. من پایگاه داده ام رو با یوزر root اجرا می کنم. شما می تونید از کاربرای دیگه استفاده کنید | |
$mysql -u 'root' < "employees.sql" -p |
This file contains 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
$DB_PASS='mypassword' | |
$export DB_PASS |
This file contains 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
import os | |
def env_var(name): | |
'''gets the environment variables''' | |
try: | |
return str(os.environ[name]) | |
except: | |
raise KeyError | |
DATABASES = { | |
'default': { | |
'ENGINE': 'django.db.backends.mysql', |
This file contains 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
$pip install mysqlclient | |
$python manage.py inspectdb > employee_app/models.py |
This file contains 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
from django.db import models | |
class Employees(models.Model): | |
emp_no = models.IntegerField(primary_key=True) | |
birth_date = models.DateField() | |
first_name = models.CharField(max_length=20) | |
last_name = models.CharField(max_length=20) | |
gender = models.CharField(max_length=1) | |
hire_date = models.DateField() | |
class Meta: | |
managed = False |
This file contains 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
$python manage.py makemigrations | |
$python manage.py migrate |
This file contains 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
INSTALLED_APPS = [ | |
'django.contrib.admin', | |
'django.contrib.auth', | |
'django.contrib.contenttypes', | |
'django.contrib.sessions', | |
'django.contrib.messages', | |
'django.contrib.staticfiles', | |
'employees_app', | |
'rest_framework', | |
] |
This file contains 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
from rest_framework import serializers | |
from .models import * | |
class EmployeeSerializer(serializers.ModelSerializer): | |
class Meta: | |
model = Employees | |
fields = "__all__" | |
class DepartmentsSerializer(serializers.ModelSerializer): | |
class Meta: | |
model = Departments | |
fields = "__all__" |
This file contains 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
from django.shortcuts import render | |
from rest_framework import viewsets | |
from .serializers import * | |
from .models import * | |
class EmployeeAPI(viewsets.ModelViewSet): | |
serializer_class = EmployeeSerializer | |
queryset = Employees.objects.all()[:5] | |
class DepartmentsAPI(viewsets.ModelViewSet): | |
serializer_class = DepartmentsSerializer | |
queryset = Departments.objects.all()[:5] |
OlderNewer