Skip to content

Instantly share code, notes, and snippets.

@moeinserpico
moeinserpico / virtulaenv
Created September 30, 2019 05:12
Virtual Environment Setups
--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
--ساخت دیتابیس در شاخه root پروژه. من پایگاه داده ام رو با یوزر root اجرا می کنم. شما می تونید از کاربرای دیگه استفاده کنید
$mysql -u 'root' < "employees.sql" -p
@moeinserpico
moeinserpico / envvar
Created September 30, 2019 05:46
craete env var for mysql root pass
$DB_PASS='mypassword'
$export DB_PASS
@moeinserpico
moeinserpico / gist:8c19cc9f53404eb71a068e48d4aabb2e
Created September 30, 2019 05:48
.یرایش Seeting جنگو
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',
@moeinserpico
moeinserpico / gist:476cf904cd6d042d9508c960d56705ec
Created September 30, 2019 05:52
Creating the Database Models using InspectDB
$pip install mysqlclient
$python manage.py inspectdb > employee_app/models.py
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
$python manage.py makemigrations
$python manage.py migrate
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'employees_app',
'rest_framework',
]
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__"
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]