Skip to content

Instantly share code, notes, and snippets.

HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
[
{
"emp_no": 10001,
"salary": 60117,
"from_date": "1986-06-26",
"to_date": "1987-06-26"
HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
{
"employees": "http://127.0.0.1:8080/api/employees/",
"departments": "http://127.0.0.1:8080/api/departments/",
"deptemp": "http://127.0.0.1:8080/api/deptemp/",
"deptmanager": "http://127.0.0.1:8080/api/deptmanager/",
"salaries": "http://127.0.0.1:8080/api/salaries/",
$python manage.py runserver 0.0.0.0:8080
from django.contrib import admin
from . import models
# Register your models here.
admin.site.register(models.Employees)
admin.site.register(models.Departments)
admin.site.register(models.DeptEmp)
admin.site.register(models.DeptManager)
admin.site.register(models.Salaries)
admin.site.register(models.Titles)
from django.contrib import admin
from django.urls import path, include
from rest_framework import routers
from employee_app import views
#Use the router to dynamically create our urls
router = routers.DefaultRouter()
router.register('employees', views.EmployeeAPI)
router.register('departments', views.DepartmentsAPI)
router.register('deptemp', views.DeptEmpAPI)
router.register('deptmanager', views.DeptManagerAPI)
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]
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__"
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'employees_app',
'rest_framework',
]
$python manage.py makemigrations
$python manage.py migrate
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