├── .gitignore ├── class_based_crud ├── api │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-310.pyc │ │ ├── admin.cpython-310.pyc │ │ ├── apps.cpython-310.pyc │ │ ├── models.cpython-310.pyc │ │ ├── serializer.cpython-310.pyc │ │ └── views.cpython-310.pyc │ ├── admin.py │ ├── apps.py │ ├── migrations │ │ ├── 0001_initial.py │ │ ├── __init__.py │ │ └── __pycache__ │ │ │ ├── 0001_initial.cpython-310.pyc │ │ │ └── __init__.cpython-310.pyc │ ├── models.py │ ├── serializer.py │ ├── tests.py │ └── views.py ├── class_based_crud │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-310.pyc │ │ ├── settings.cpython-310.pyc │ │ ├── urls.cpython-310.pyc │ │ └── wsgi.cpython-310.pyc │ ├── asgi.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── db.sqlite3 ├── manage.py └── myapp.py ├── crud ├── class_base │ ├── api │ │ ├── __init__.py │ │ ├── __pycache__ │ │ │ ├── __init__.cpython-310.pyc │ │ │ ├── admin.cpython-310.pyc │ │ │ ├── apps.cpython-310.pyc │ │ │ ├── models.cpython-310.pyc │ │ │ ├── serializers.cpython-310.pyc │ │ │ └── views.cpython-310.pyc │ │ ├── admin.py │ │ ├── apps.py │ │ ├── migrations │ │ │ ├── 0001_initial.py │ │ │ ├── __init__.py │ │ │ └── __pycache__ │ │ │ │ ├── 0001_initial.cpython-310.pyc │ │ │ │ └── __init__.cpython-310.pyc │ │ ├── models.py │ │ ├── serializers.py │ │ ├── tests.py │ │ └── views.py │ ├── class_base │ │ ├── __init__.py │ │ ├── __pycache__ │ │ │ ├── __init__.cpython-310.pyc │ │ │ ├── settings.cpython-310.pyc │ │ │ ├── urls.cpython-310.pyc │ │ │ └── wsgi.cpython-310.pyc │ │ ├── asgi.py │ │ ├── settings.py │ │ ├── urls.py │ │ └── wsgi.py │ ├── db.sqlite3 │ ├── manage.py │ └── myapp.py └── function_base │ ├── api │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-310.pyc │ │ ├── admin.cpython-310.pyc │ │ ├── apps.cpython-310.pyc │ │ ├── models.cpython-310.pyc │ │ ├── serializers.cpython-310.pyc │ │ └── views.cpython-310.pyc │ ├── admin.py │ ├── apps.py │ ├── migrations │ │ ├── 0001_initial.py │ │ ├── __init__.py │ │ └── __pycache__ │ │ │ ├── 0001_initial.cpython-310.pyc │ │ │ └── __init__.cpython-310.pyc │ ├── models.py │ ├── serializers.py │ ├── tests.py │ └── views.py │ ├── crud │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-310.pyc │ │ ├── settings.cpython-310.pyc │ │ ├── urls.cpython-310.pyc │ │ └── wsgi.cpython-310.pyc │ ├── asgi.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py │ ├── db.sqlite3 │ ├── manage.py │ └── myapp.py ├── deserialize ├── api │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-310.pyc │ │ ├── admin.cpython-310.pyc │ │ ├── apps.cpython-310.pyc │ │ ├── models.cpython-310.pyc │ │ ├── serializers.cpython-310.pyc │ │ └── views.cpython-310.pyc │ ├── admin.py │ ├── apps.py │ ├── migrations │ │ ├── 0001_initial.py │ │ ├── __init__.py │ │ └── __pycache__ │ │ │ ├── 0001_initial.cpython-310.pyc │ │ │ └── __init__.cpython-310.pyc │ ├── models.py │ ├── serializers.py │ ├── tests.py │ └── views.py ├── db.sqlite3 ├── deserialize │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-310.pyc │ │ ├── settings.cpython-310.pyc │ │ ├── urls.cpython-310.pyc │ │ └── wsgi.cpython-310.pyc │ ├── asgi.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── manage.py └── myapp.py ├── function_based_crud ├── api │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-310.pyc │ │ ├── admin.cpython-310.pyc │ │ ├── apps.cpython-310.pyc │ │ ├── models.cpython-310.pyc │ │ ├── serializer.cpython-310.pyc │ │ └── views.cpython-310.pyc │ ├── admin.py │ ├── apps.py │ ├── migrations │ │ ├── 0001_initial.py │ │ ├── __init__.py │ │ └── __pycache__ │ │ │ ├── 0001_initial.cpython-310.pyc │ │ │ └── __init__.cpython-310.pyc │ ├── models.py │ ├── serializer.py │ ├── tests.py │ └── views.py ├── db.sqlite3 ├── function_based_crud │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-310.pyc │ │ ├── settings.cpython-310.pyc │ │ ├── urls.cpython-310.pyc │ │ └── wsgi.cpython-310.pyc │ ├── asgi.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── manage.py └── myapp.py ├── generic_api ├── api_view │ ├── api │ │ ├── __init__.py │ │ ├── __pycache__ │ │ │ ├── __init__.cpython-310.pyc │ │ │ ├── admin.cpython-310.pyc │ │ │ ├── apps.cpython-310.pyc │ │ │ ├── models.cpython-310.pyc │ │ │ ├── serializer.cpython-310.pyc │ │ │ └── views.cpython-310.pyc │ │ ├── admin.py │ │ ├── apps.py │ │ ├── migrations │ │ │ ├── 0001_initial.py │ │ │ ├── __init__.py │ │ │ └── __pycache__ │ │ │ │ ├── 0001_initial.cpython-310.pyc │ │ │ │ └── __init__.cpython-310.pyc │ │ ├── models.py │ │ ├── serializer.py │ │ ├── tests.py │ │ └── views.py │ ├── api_view │ │ ├── __init__.py │ │ ├── __pycache__ │ │ │ ├── __init__.cpython-310.pyc │ │ │ ├── settings.cpython-310.pyc │ │ │ ├── urls.cpython-310.pyc │ │ │ └── wsgi.cpython-310.pyc │ │ ├── asgi.py │ │ ├── settings.py │ │ ├── urls.py │ │ └── wsgi.py │ ├── db.sqlite3 │ └── manage.py └── mixin │ ├── api │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-310.pyc │ │ ├── admin.cpython-310.pyc │ │ ├── apps.cpython-310.pyc │ │ ├── models.cpython-310.pyc │ │ ├── serializer.cpython-310.pyc │ │ └── views.cpython-310.pyc │ ├── admin.py │ ├── apps.py │ ├── migrations │ │ ├── 0001_initial.py │ │ ├── __init__.py │ │ └── __pycache__ │ │ │ ├── 0001_initial.cpython-310.pyc │ │ │ └── __init__.cpython-310.pyc │ ├── models.py │ ├── serializer.py │ ├── tests.py │ └── views.py │ ├── db.sqlite3 │ ├── generic_api │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-310.pyc │ │ ├── settings.cpython-310.pyc │ │ ├── urls.cpython-310.pyc │ │ └── wsgi.cpython-310.pyc │ ├── asgi.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py │ ├── manage.py │ └── myapp.py ├── model_serializer ├── api │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-310.pyc │ │ ├── admin.cpython-310.pyc │ │ ├── apps.cpython-310.pyc │ │ ├── models.cpython-310.pyc │ │ ├── serializers.cpython-310.pyc │ │ └── views.cpython-310.pyc │ ├── admin.py │ ├── apps.py │ ├── migrations │ │ ├── 0001_initial.py │ │ ├── __init__.py │ │ └── __pycache__ │ │ │ ├── 0001_initial.cpython-310.pyc │ │ │ └── __init__.cpython-310.pyc │ ├── models.py │ ├── serializers.py │ ├── tests.py │ └── views.py ├── db.sqlite3 ├── manage.py ├── model_serializer │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-310.pyc │ │ ├── settings.cpython-310.pyc │ │ ├── urls.cpython-310.pyc │ │ └── wsgi.cpython-310.pyc │ ├── asgi.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py └── myapp.py ├── serialize ├── api │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-310.pyc │ │ ├── admin.cpython-310.pyc │ │ ├── apps.cpython-310.pyc │ │ ├── models.cpython-310.pyc │ │ ├── serializer.cpython-310.pyc │ │ └── views.cpython-310.pyc │ ├── admin.py │ ├── apps.py │ ├── migrations │ │ ├── 0001_initial.py │ │ ├── __init__.py │ │ └── __pycache__ │ │ │ ├── 0001_initial.cpython-310.pyc │ │ │ └── __init__.cpython-310.pyc │ ├── models.py │ ├── serializer.py │ ├── tests.py │ └── views.py ├── db.sqlite3 ├── manage.py └── serialize │ ├── __init__.py │ ├── __pycache__ │ ├── __init__.cpython-310.pyc │ ├── settings.cpython-310.pyc │ ├── urls.cpython-310.pyc │ └── wsgi.cpython-310.pyc │ ├── asgi.py │ ├── myapp.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── validation ├── api │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-310.pyc │ │ ├── admin.cpython-310.pyc │ │ ├── apps.cpython-310.pyc │ │ ├── models.cpython-310.pyc │ │ ├── serializers.cpython-310.pyc │ │ └── views.cpython-310.pyc │ ├── admin.py │ ├── apps.py │ ├── migrations │ │ ├── 0001_initial.py │ │ ├── __init__.py │ │ └── __pycache__ │ │ │ ├── 0001_initial.cpython-310.pyc │ │ │ └── __init__.cpython-310.pyc │ ├── models.py │ ├── serializers.py │ ├── tests.py │ └── views.py ├── db.sqlite3 ├── manage.py ├── myapp.py └── validation │ ├── __init__.py │ ├── __pycache__ │ ├── __init__.cpython-310.pyc │ ├── settings.cpython-310.pyc │ ├── urls.cpython-310.pyc │ └── wsgi.cpython-310.pyc │ ├── asgi.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py └── view_set ├── api ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-310.pyc │ ├── admin.cpython-310.pyc │ ├── apps.cpython-310.pyc │ ├── models.cpython-310.pyc │ ├── serialize.cpython-310.pyc │ └── views.cpython-310.pyc ├── admin.py ├── apps.py ├── migrations │ ├── 0001_initial.py │ ├── __init__.py │ └── __pycache__ │ │ ├── 0001_initial.cpython-310.pyc │ │ └── __init__.cpython-310.pyc ├── models.py ├── serialize.py ├── tests.py └── views.py ├── db.sqlite3 ├── manage.py └── view_set ├── __init__.py ├── __pycache__ ├── __init__.cpython-310.pyc ├── settings.cpython-310.pyc ├── urls.cpython-310.pyc └── wsgi.cpython-310.pyc ├── asgi.py ├── settings.py ├── urls.py └── wsgi.py /.gitignore: -------------------------------------------------------------------------------- 1 | \venv 2 | -------------------------------------------------------------------------------- /class_based_crud/api/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/class_based_crud/api/__init__.py -------------------------------------------------------------------------------- /class_based_crud/api/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/class_based_crud/api/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /class_based_crud/api/__pycache__/admin.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/class_based_crud/api/__pycache__/admin.cpython-310.pyc -------------------------------------------------------------------------------- /class_based_crud/api/__pycache__/apps.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/class_based_crud/api/__pycache__/apps.cpython-310.pyc -------------------------------------------------------------------------------- /class_based_crud/api/__pycache__/models.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/class_based_crud/api/__pycache__/models.cpython-310.pyc -------------------------------------------------------------------------------- /class_based_crud/api/__pycache__/serializer.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/class_based_crud/api/__pycache__/serializer.cpython-310.pyc -------------------------------------------------------------------------------- /class_based_crud/api/__pycache__/views.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/class_based_crud/api/__pycache__/views.cpython-310.pyc -------------------------------------------------------------------------------- /class_based_crud/api/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from api.models import Student 3 | # Register your models here. 4 | 5 | @admin.register(Student) 6 | class StudentAdmin(admin.ModelAdmin): 7 | 8 | list_display = ['id', 'name', 'roll', 'city'] -------------------------------------------------------------------------------- /class_based_crud/api/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class ApiConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'api' 7 | -------------------------------------------------------------------------------- /class_based_crud/api/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.5 on 2023-01-31 07:24 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Student', 16 | fields=[ 17 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('name', models.CharField(max_length=50)), 19 | ('roll', models.IntegerField()), 20 | ('city', models.CharField(max_length=50)), 21 | ], 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /class_based_crud/api/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/class_based_crud/api/migrations/__init__.py -------------------------------------------------------------------------------- /class_based_crud/api/migrations/__pycache__/0001_initial.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/class_based_crud/api/migrations/__pycache__/0001_initial.cpython-310.pyc -------------------------------------------------------------------------------- /class_based_crud/api/migrations/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/class_based_crud/api/migrations/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /class_based_crud/api/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | 5 | class Student(models.Model): 6 | 7 | name = models.CharField(max_length=50) 8 | roll = models.IntegerField() 9 | city = models.CharField(max_length=50) -------------------------------------------------------------------------------- /class_based_crud/api/serializer.py: -------------------------------------------------------------------------------- 1 | from api.models import Student 2 | from rest_framework import serializers 3 | 4 | class StudentSerializer(serializers.ModelSerializer): 5 | 6 | class Meta: 7 | 8 | model = Student 9 | fields = ['id', 'name', 'roll', 'city'] -------------------------------------------------------------------------------- /class_based_crud/api/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /class_based_crud/api/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | from rest_framework.views import APIView 3 | from api.models import Student 4 | from rest_framework.response import Response 5 | from api.serializer import StudentSerializer 6 | from rest_framework import status 7 | 8 | # Create your views here. 9 | 10 | class StudentAPI(APIView): 11 | def get(self, request, pk=None, formate= None): 12 | id = pk 13 | 14 | if id is not None: 15 | stu = Student.objects.get(id=id) 16 | serializer = StudentSerializer(stu) 17 | return Response(serializer.data) 18 | 19 | stu = Student.objects.all() 20 | serializer = StudentSerializer(stu, many= True) 21 | return Response(serializer.data) 22 | 23 | def post(self, request, formate= None): 24 | 25 | serializer = StudentSerializer(data = request.data) 26 | if serializer.is_valid(): 27 | serializer.save() 28 | return Response ({'msg':'Data Created'}, status=status.HTTP_201_CREATED) 29 | return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) 30 | 31 | def put(self, request, pk, formate= None): 32 | id = pk 33 | stu = Student.objects.get(pk=id) 34 | serializer = StudentSerializer(stu, data= request.data) 35 | if serializer.is_valid(): 36 | serializer.save() 37 | return Response({'msg':'Data Updated'}, status=status.HTTP_200_OK) 38 | return Response(serializer.errors) 39 | 40 | def patch(self, request, pk, formate= None): 41 | id = pk 42 | stu = Student.objects.get(pk=id) 43 | serializer = StudentSerializer(stu, data= request.data, partial=True) 44 | if serializer.is_valid(): 45 | serializer.save() 46 | return Response({'msg':'Data Updated'}) 47 | return Response(serializer.errors) 48 | 49 | def delete(self, request, pk, formate= None): 50 | id = pk 51 | stu = Student.objects.get(pk=id) 52 | stu.delete() 53 | return Response({'msg': 'Data Deleted'}) 54 | -------------------------------------------------------------------------------- /class_based_crud/class_based_crud/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/class_based_crud/class_based_crud/__init__.py -------------------------------------------------------------------------------- /class_based_crud/class_based_crud/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/class_based_crud/class_based_crud/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /class_based_crud/class_based_crud/__pycache__/settings.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/class_based_crud/class_based_crud/__pycache__/settings.cpython-310.pyc -------------------------------------------------------------------------------- /class_based_crud/class_based_crud/__pycache__/urls.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/class_based_crud/class_based_crud/__pycache__/urls.cpython-310.pyc -------------------------------------------------------------------------------- /class_based_crud/class_based_crud/__pycache__/wsgi.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/class_based_crud/class_based_crud/__pycache__/wsgi.cpython-310.pyc -------------------------------------------------------------------------------- /class_based_crud/class_based_crud/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for class_based_crud project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'class_based_crud.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /class_based_crud/class_based_crud/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for class_based_crud project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.1.5. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'django-insecure-hyl7=!1!69w3yhhp5r)+k--)d9m$8spamgq17@)(p_%ys70mjp' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'rest_framework', 41 | 'api', 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'class_based_crud.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'class_based_crud.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/4.1/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': BASE_DIR / 'db.sqlite3', 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/4.1/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'UTC' 111 | 112 | USE_I18N = True 113 | 114 | USE_TZ = True 115 | 116 | 117 | # Static files (CSS, JavaScript, Images) 118 | # https://docs.djangoproject.com/en/4.1/howto/static-files/ 119 | 120 | STATIC_URL = 'static/' 121 | 122 | # Default primary key field type 123 | # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field 124 | 125 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 126 | -------------------------------------------------------------------------------- /class_based_crud/class_based_crud/urls.py: -------------------------------------------------------------------------------- 1 | """class_based_crud URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/4.1/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path 18 | from api import views 19 | urlpatterns = [ 20 | path('admin/', admin.site.urls), 21 | path('student_api', views.StudentAPI.as_view()), 22 | path('student_api/', views.StudentAPI.as_view()) 23 | ] 24 | -------------------------------------------------------------------------------- /class_based_crud/class_based_crud/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for class_based_crud project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'class_based_crud.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /class_based_crud/db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/class_based_crud/db.sqlite3 -------------------------------------------------------------------------------- /class_based_crud/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'class_based_crud.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /class_based_crud/myapp.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import json 3 | 4 | URL = 'http://127.0.0.1:8000/student_api' 5 | 6 | def get_data(id = None): 7 | data = {} 8 | 9 | if id is not None: 10 | data = {'id': id} 11 | 12 | headers = {'content-Type': 'application/json'} 13 | json_data = json.dumps(data) 14 | r = requests.get(url= URL, headers= headers, data= json_data) 15 | data = r.json() 16 | print(data) 17 | 18 | get_data() 19 | 20 | def post_data(): 21 | data = { 22 | 'name': 'rohan', 23 | 'roll':'120', 24 | 'city':'Surat' 25 | } 26 | 27 | headers = {'content-Type': 'application/json'} 28 | json_data = json.dumps(data) 29 | r = requests.post(url= URL, headers=headers, data= json_data) 30 | data= r.json() 31 | print(data) 32 | 33 | # post_data() 34 | 35 | def update_data(): 36 | data = { 37 | 'id':3, 38 | 'name': 'Rohan', 39 | 'city':'Ahmadabad', 40 | 'roll': '104' 41 | } 42 | 43 | headers = {'content-Type': 'application/json'} 44 | json_data = json.dumps(data) 45 | r = requests.put(url= URL, headers=headers, data= json_data) 46 | data= r.json() 47 | print(data) 48 | 49 | # update_data() 50 | 51 | def delete_data(): 52 | data = { 53 | 'id': 3 54 | } 55 | 56 | headers = {'content-Type': 'application/json'} 57 | json_data = json.dumps(data) 58 | r = requests.delete(url= URL, headers=headers, data= json_data) 59 | data = r.json() 60 | print(data) 61 | 62 | # delete_data() -------------------------------------------------------------------------------- /crud/class_base/api/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/class_base/api/__init__.py -------------------------------------------------------------------------------- /crud/class_base/api/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/class_base/api/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /crud/class_base/api/__pycache__/admin.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/class_base/api/__pycache__/admin.cpython-310.pyc -------------------------------------------------------------------------------- /crud/class_base/api/__pycache__/apps.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/class_base/api/__pycache__/apps.cpython-310.pyc -------------------------------------------------------------------------------- /crud/class_base/api/__pycache__/models.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/class_base/api/__pycache__/models.cpython-310.pyc -------------------------------------------------------------------------------- /crud/class_base/api/__pycache__/serializers.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/class_base/api/__pycache__/serializers.cpython-310.pyc -------------------------------------------------------------------------------- /crud/class_base/api/__pycache__/views.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/class_base/api/__pycache__/views.cpython-310.pyc -------------------------------------------------------------------------------- /crud/class_base/api/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from api.models import Student 3 | # Register your models here. 4 | 5 | @admin.register(Student) 6 | class StudentAdmin(admin.ModelAdmin): 7 | 8 | list_display = ['id', 'name', 'roll', 'city'] -------------------------------------------------------------------------------- /crud/class_base/api/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class ApiConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'api' 7 | -------------------------------------------------------------------------------- /crud/class_base/api/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.5 on 2023-01-30 12:12 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Student', 16 | fields=[ 17 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('name', models.CharField(max_length=50)), 19 | ('roll', models.IntegerField()), 20 | ('city', models.CharField(max_length=50)), 21 | ], 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /crud/class_base/api/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/class_base/api/migrations/__init__.py -------------------------------------------------------------------------------- /crud/class_base/api/migrations/__pycache__/0001_initial.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/class_base/api/migrations/__pycache__/0001_initial.cpython-310.pyc -------------------------------------------------------------------------------- /crud/class_base/api/migrations/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/class_base/api/migrations/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /crud/class_base/api/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | 5 | class Student(models.Model): 6 | 7 | name = models.CharField(max_length=50) 8 | roll = models.IntegerField() 9 | city = models.CharField(max_length=50) -------------------------------------------------------------------------------- /crud/class_base/api/serializers.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | from api.models import Student 3 | 4 | class StudentSerializer(serializers.Serializer): 5 | 6 | name = serializers.CharField(max_length=50) 7 | roll = serializers.IntegerField() 8 | city = serializers.CharField(max_length=50) 9 | 10 | def create(self, validated_data): 11 | return Student.objects.create(**validated_data) 12 | 13 | def update(self, instance, validated_data): 14 | print(instance.name) 15 | instance.name = validated_data.get('name', instance.name) 16 | instance.roll = validated_data.get('roll', instance.roll) 17 | instance.city = validated_data.get('city', instance.city) 18 | instance.save() 19 | return instance -------------------------------------------------------------------------------- /crud/class_base/api/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /crud/class_base/api/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | import io 3 | from rest_framework.parsers import JSONParser 4 | from api.models import Student 5 | from api.serializers import StudentSerializer 6 | from rest_framework.renderers import JSONRenderer 7 | from django.http import HttpResponse 8 | from django.views.decorators.csrf import csrf_exempt 9 | from django.utils.decorators import method_decorator 10 | from django.views import View 11 | 12 | # Create your views here. 13 | @method_decorator(csrf_exempt, name='dispatch') 14 | class StudentAPI(View): 15 | def get(self, request, *args, **kwargs): 16 | json_data = request.body 17 | stream = io.BytesIO(json_data) 18 | python_data = JSONParser().parse(stream) 19 | id = python_data.get('id', None) 20 | 21 | if id is None: 22 | stu = Student.objects.get(id = id) 23 | serializer = StudentSerializer(stu) 24 | json_data = JSONRenderer().render(serializer.data) 25 | return HttpResponse(json_data, content_type = 'application/json') 26 | 27 | stu = Student.objects.all() 28 | serializer = StudentSerializer(stu, many = True) 29 | json_data = JSONRenderer().render(serializer.data) 30 | return HttpResponse(json_data, content_type = 'application/json') 31 | 32 | def post(self, request, *args, **kwargs): 33 | 34 | json_data = request.body 35 | stream = io.BytesIO(json_data) 36 | python_data = JSONParser().parse(stream) 37 | serializer = StudentSerializer(data= python_data) 38 | 39 | if serializer.is_valid(): 40 | serializer.save() 41 | res = {"msg":"data created"} 42 | json_data = JSONRenderer().render(res) 43 | return HttpResponse(json_data, content_type = '/application/json') 44 | 45 | json_data = JSONRenderer().render(serializer.errors) 46 | return HttpResponse(json_data, content_type = '/application/json') 47 | 48 | def put(self, request, *args, **kwargs): 49 | 50 | json_data = request.body 51 | stream = io.BytesIO(json_data) 52 | python_data = JSONParser().parse(stream) 53 | id=python_data.get('id') 54 | stu = Student.objects.get(id=id) 55 | serializer = StudentSerializer(stu, data= python_data, partial = True) 56 | 57 | if serializer.is_valid(): 58 | serializer.save() 59 | res = {"msg": "data updated"} 60 | json_data = JSONRenderer().render(res) 61 | return HttpResponse(json_data, content_type = 'application/json') 62 | 63 | json_data = JSONRenderer().render(res) 64 | return HttpResponse(json_data, content_type = 'application/json') 65 | 66 | def delete(self, request, *args, **kwargs): 67 | 68 | json_data = request.body 69 | stream = io.BytesIO(json_data) 70 | python_data = JSONParser().parse(stream) 71 | id = python_data.get('id') 72 | stu = Student.objects.get(id = id) 73 | stu.delete() 74 | res = {"mes": "data deleted"} 75 | json_data = JSONRenderer().render(res) 76 | return HttpResponse(json_data, content_type = 'application/json') 77 | -------------------------------------------------------------------------------- /crud/class_base/class_base/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/class_base/class_base/__init__.py -------------------------------------------------------------------------------- /crud/class_base/class_base/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/class_base/class_base/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /crud/class_base/class_base/__pycache__/settings.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/class_base/class_base/__pycache__/settings.cpython-310.pyc -------------------------------------------------------------------------------- /crud/class_base/class_base/__pycache__/urls.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/class_base/class_base/__pycache__/urls.cpython-310.pyc -------------------------------------------------------------------------------- /crud/class_base/class_base/__pycache__/wsgi.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/class_base/class_base/__pycache__/wsgi.cpython-310.pyc -------------------------------------------------------------------------------- /crud/class_base/class_base/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for class_base project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'class_base.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /crud/class_base/class_base/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for class_base project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.1.5. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'django-insecure-rf2naifu576^a(8v2=6(=c4c7-%&*ywkv05@1i+400d^ggp+cz' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'rest_framework', 41 | 'api', 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'class_base.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'class_base.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/4.1/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': BASE_DIR / 'db.sqlite3', 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/4.1/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'UTC' 111 | 112 | USE_I18N = True 113 | 114 | USE_TZ = True 115 | 116 | 117 | # Static files (CSS, JavaScript, Images) 118 | # https://docs.djangoproject.com/en/4.1/howto/static-files/ 119 | 120 | STATIC_URL = 'static/' 121 | 122 | # Default primary key field type 123 | # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field 124 | 125 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 126 | -------------------------------------------------------------------------------- /crud/class_base/class_base/urls.py: -------------------------------------------------------------------------------- 1 | """class_base URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/4.1/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path 18 | from api import views 19 | 20 | urlpatterns = [ 21 | path('admin/', admin.site.urls), 22 | path('student_api', views.StudentAPI.as_view()) 23 | ] 24 | -------------------------------------------------------------------------------- /crud/class_base/class_base/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for class_base project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'class_base.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /crud/class_base/db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/class_base/db.sqlite3 -------------------------------------------------------------------------------- /crud/class_base/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'class_base.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /crud/class_base/myapp.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import json 3 | 4 | URL = 'http://127.0.0.1:8000/student_api' 5 | 6 | def get_data(id = None): 7 | data = {} 8 | 9 | if id is not None: 10 | data = {'id': id} 11 | 12 | json_data = json.dumps(data) 13 | r = requests.get(url= URL, data= json_data) 14 | data = r.json() 15 | print(data) 16 | 17 | get_data(1) 18 | 19 | def post_data(): 20 | data = { 21 | 'name': 'ravi', 22 | 'roll':'104', 23 | 'city':'Dhanbad' 24 | } 25 | 26 | json_data = json.dumps(data) 27 | r = requests.post(url= URL, data= json_data) 28 | data= r.json() 29 | print(data) 30 | 31 | post_data() 32 | 33 | def update_data(): 34 | data = { 35 | 'id':3, 36 | 'name': 'Rohan', 37 | 'city':'Ahmadabad', 38 | 'roll': '104' 39 | } 40 | 41 | json_data = json.dumps(data) 42 | r = requests.put(url= URL, data= json_data) 43 | data= r.json() 44 | print(data) 45 | 46 | # update_data() 47 | 48 | def delete_data(): 49 | data = { 50 | 'id': 4 51 | } 52 | 53 | json_data = json.dumps(data) 54 | r = requests.delete(url= URL, data= json_data) 55 | data = r.json() 56 | print(data) 57 | 58 | # delete_data() -------------------------------------------------------------------------------- /crud/function_base/api/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/function_base/api/__init__.py -------------------------------------------------------------------------------- /crud/function_base/api/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/function_base/api/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /crud/function_base/api/__pycache__/admin.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/function_base/api/__pycache__/admin.cpython-310.pyc -------------------------------------------------------------------------------- /crud/function_base/api/__pycache__/apps.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/function_base/api/__pycache__/apps.cpython-310.pyc -------------------------------------------------------------------------------- /crud/function_base/api/__pycache__/models.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/function_base/api/__pycache__/models.cpython-310.pyc -------------------------------------------------------------------------------- /crud/function_base/api/__pycache__/serializers.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/function_base/api/__pycache__/serializers.cpython-310.pyc -------------------------------------------------------------------------------- /crud/function_base/api/__pycache__/views.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/function_base/api/__pycache__/views.cpython-310.pyc -------------------------------------------------------------------------------- /crud/function_base/api/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from api.models import Student 3 | # Register your models here. 4 | 5 | @admin.register(Student) 6 | class StudentAdmin(admin.ModelAdmin): 7 | 8 | list_display = ['id', 'name', 'roll', 'city'] -------------------------------------------------------------------------------- /crud/function_base/api/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class ApiConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'api' 7 | -------------------------------------------------------------------------------- /crud/function_base/api/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.5 on 2023-01-30 06:29 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Student', 16 | fields=[ 17 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('name', models.CharField(max_length=50)), 19 | ('roll', models.IntegerField()), 20 | ('city', models.CharField(max_length=50)), 21 | ], 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /crud/function_base/api/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/function_base/api/migrations/__init__.py -------------------------------------------------------------------------------- /crud/function_base/api/migrations/__pycache__/0001_initial.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/function_base/api/migrations/__pycache__/0001_initial.cpython-310.pyc -------------------------------------------------------------------------------- /crud/function_base/api/migrations/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/function_base/api/migrations/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /crud/function_base/api/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | 5 | class Student(models.Model): 6 | 7 | name = models.CharField(max_length=50) 8 | roll = models.IntegerField() 9 | city = models.CharField(max_length=50) -------------------------------------------------------------------------------- /crud/function_base/api/serializers.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | from api.models import Student 3 | 4 | class StudentSerializer(serializers.Serializer): 5 | 6 | name = serializers.CharField(max_length=50) 7 | roll = serializers.IntegerField() 8 | city = serializers.CharField(max_length=50) 9 | 10 | def create(self, validated_data): 11 | return Student.objects.create(**validated_data) 12 | 13 | def update(self, instance, validated_data): 14 | print(instance.name) 15 | instance.name = validated_data.get('name', instance.name) 16 | instance.roll = validated_data.get('roll', instance.roll) 17 | instance.city = validated_data.get('city', instance.city) 18 | instance.save() 19 | return instance -------------------------------------------------------------------------------- /crud/function_base/api/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /crud/function_base/api/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | import io 3 | from rest_framework.parsers import JSONParser 4 | from api.models import Student 5 | from api.serializers import StudentSerializer 6 | from rest_framework.renderers import JSONRenderer 7 | from django.http import HttpResponse 8 | from django.views.decorators.csrf import csrf_exempt 9 | 10 | # Create your views here. 11 | 12 | @csrf_exempt 13 | def student_api(request): 14 | 15 | # Read 16 | if request.method == 'GET': 17 | json_data = request.body 18 | stream = io.BytesIO(json_data) 19 | python_data = JSONParser().parse(stream) 20 | id = python_data.get('id', None) 21 | 22 | if id is None: 23 | stu = Student.objects.get(id = id) 24 | serializer = StudentSerializer(stu) 25 | json_data = JSONRenderer().render(serializer.data) 26 | return HttpResponse(json_data, content_type = 'application/json') 27 | 28 | stu = Student.objects.all() 29 | serializer = StudentSerializer(stu, many = True) 30 | json_data = JSONRenderer().render(serializer.data) 31 | return HttpResponse(json_data, content_type = 'application/json') 32 | 33 | # Create 34 | if request.method == 'POST': 35 | json_data = request.body 36 | stream = io.BytesIO(json_data) 37 | python_data = JSONParser().parse(stream) 38 | serializer = StudentSerializer(data= python_data) 39 | 40 | if serializer.is_valid(): 41 | serializer.save() 42 | res = {"msg":"data created"} 43 | json_data = JSONRenderer().render(res) 44 | return HttpResponse(json_data, content_type = '/application/json') 45 | 46 | json_data = JSONRenderer().render(serializer.errors) 47 | return HttpResponse(json_data, content_type = '/application/json') 48 | 49 | # Update 50 | if request.method == 'PUT': 51 | json_data = request.body 52 | stream = io.BytesIO(json_data) 53 | python_data = JSONParser().parse(stream) 54 | id=python_data.get('id') 55 | stu = Student.objects.get(id=id) 56 | serializer = StudentSerializer(stu, data= python_data, partial = True) 57 | 58 | if serializer.is_valid(): 59 | serializer.save() 60 | res = {"msg": "data updated"} 61 | json_data = JSONRenderer().render(res) 62 | return HttpResponse(json_data, content_type = 'application/json') 63 | 64 | json_data = JSONRenderer().render(res) 65 | return HttpResponse(json_data, content_type = 'application/json') 66 | 67 | # Delete 68 | if request.method == 'DELETE': 69 | json_data = request.body 70 | stream = io.BytesIO(json_data) 71 | python_data = JSONParser().parse(stream) 72 | id = python_data.get('id') 73 | stu = Student.objects.get(id = id) 74 | stu.delete() 75 | res = {"mes": "data deleted"} 76 | json_data = JSONRenderer().render(res) 77 | # return HttpResponse(json_data, content_type = 'application/json') 78 | 79 | -------------------------------------------------------------------------------- /crud/function_base/crud/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/function_base/crud/__init__.py -------------------------------------------------------------------------------- /crud/function_base/crud/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/function_base/crud/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /crud/function_base/crud/__pycache__/settings.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/function_base/crud/__pycache__/settings.cpython-310.pyc -------------------------------------------------------------------------------- /crud/function_base/crud/__pycache__/urls.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/function_base/crud/__pycache__/urls.cpython-310.pyc -------------------------------------------------------------------------------- /crud/function_base/crud/__pycache__/wsgi.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/function_base/crud/__pycache__/wsgi.cpython-310.pyc -------------------------------------------------------------------------------- /crud/function_base/crud/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for crud project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'crud.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /crud/function_base/crud/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for crud project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.1.5. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'django-insecure-*0gpm47t($okw^bhb4lg!=gekylp+bhaj)t5ks3*u3epwtv1ty' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'rest_framework', 41 | 'api' 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'crud.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'crud.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/4.1/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': BASE_DIR / 'db.sqlite3', 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/4.1/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'UTC' 111 | 112 | USE_I18N = True 113 | 114 | USE_TZ = True 115 | 116 | 117 | # Static files (CSS, JavaScript, Images) 118 | # https://docs.djangoproject.com/en/4.1/howto/static-files/ 119 | 120 | STATIC_URL = 'static/' 121 | 122 | # Default primary key field type 123 | # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field 124 | 125 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 126 | -------------------------------------------------------------------------------- /crud/function_base/crud/urls.py: -------------------------------------------------------------------------------- 1 | """crud URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/4.1/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path 18 | from api import views 19 | 20 | urlpatterns = [ 21 | path('admin/', admin.site.urls), 22 | path('student_api', views.student_api) 23 | ] 24 | -------------------------------------------------------------------------------- /crud/function_base/crud/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for crud project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'crud.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /crud/function_base/db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/crud/function_base/db.sqlite3 -------------------------------------------------------------------------------- /crud/function_base/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'crud.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /crud/function_base/myapp.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import json 3 | 4 | URL = 'http://127.0.0.1:8000/student_api' 5 | 6 | 7 | def get_data(id=None): 8 | data = {} 9 | 10 | if id is not None: 11 | data = {'id': id} 12 | 13 | json_data = json.dumps(data) 14 | r = requests.get(url=URL, data=json_data) 15 | data = r.json() 16 | print(data) 17 | 18 | # get_data(1) 19 | 20 | 21 | def post_data(): 22 | data = { 23 | 'name': 'ravi', 24 | 'roll': '104', 25 | 'city': 'Dhanbad' 26 | } 27 | 28 | json_data = json.dumps(data) 29 | r = requests.post(url=URL, data=json_data) 30 | data = r.json() 31 | print(data) 32 | 33 | # post_data() 34 | 35 | 36 | def update_data(): 37 | data = { 38 | 'id': 3, 39 | 'name': 'Rohan', 40 | 'city': 'Ahmadabad', 41 | 'roll': '104' 42 | } 43 | 44 | json_data = json.dumps(data) 45 | r = requests.put(url=URL, data=json_data) 46 | data = r.json() 47 | print(data) 48 | 49 | 50 | update_data() 51 | 52 | 53 | def delete_data(): 54 | data = { 55 | 'id': 4 56 | } 57 | 58 | json_data = json.dumps(data) 59 | r = requests.delete(url=URL, data=json_data) 60 | data = r.json() 61 | print(data) 62 | 63 | # delete_data() 64 | -------------------------------------------------------------------------------- /deserialize/api/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/deserialize/api/__init__.py -------------------------------------------------------------------------------- /deserialize/api/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/deserialize/api/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /deserialize/api/__pycache__/admin.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/deserialize/api/__pycache__/admin.cpython-310.pyc -------------------------------------------------------------------------------- /deserialize/api/__pycache__/apps.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/deserialize/api/__pycache__/apps.cpython-310.pyc -------------------------------------------------------------------------------- /deserialize/api/__pycache__/models.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/deserialize/api/__pycache__/models.cpython-310.pyc -------------------------------------------------------------------------------- /deserialize/api/__pycache__/serializers.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/deserialize/api/__pycache__/serializers.cpython-310.pyc -------------------------------------------------------------------------------- /deserialize/api/__pycache__/views.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/deserialize/api/__pycache__/views.cpython-310.pyc -------------------------------------------------------------------------------- /deserialize/api/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | 5 | from api.models import Student 6 | 7 | @admin.register(Student) 8 | class StudentAdmin(admin.ModelAdmin): 9 | 10 | list_display = ['id', 'name', 'roll', 'city'] -------------------------------------------------------------------------------- /deserialize/api/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class ApiConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'api' 7 | -------------------------------------------------------------------------------- /deserialize/api/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.5 on 2023-01-30 05:22 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Student', 16 | fields=[ 17 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('name', models.CharField(max_length=50)), 19 | ('roll', models.IntegerField()), 20 | ('city', models.CharField(max_length=50)), 21 | ], 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /deserialize/api/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/deserialize/api/migrations/__init__.py -------------------------------------------------------------------------------- /deserialize/api/migrations/__pycache__/0001_initial.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/deserialize/api/migrations/__pycache__/0001_initial.cpython-310.pyc -------------------------------------------------------------------------------- /deserialize/api/migrations/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/deserialize/api/migrations/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /deserialize/api/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | 5 | class Student(models.Model): 6 | name = models.CharField( max_length=50) 7 | roll = models.IntegerField() 8 | city = models.CharField(max_length=50) -------------------------------------------------------------------------------- /deserialize/api/serializers.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | from api.models import Student 3 | 4 | class StudentSerializer(serializers.Serializer): 5 | 6 | name = serializers.CharField( max_length=50) 7 | roll = serializers.IntegerField() 8 | city = serializers.CharField(max_length=50) 9 | 10 | def create(self, validate_data): 11 | return Student.objects.create(**validate_data) -------------------------------------------------------------------------------- /deserialize/api/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /deserialize/api/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | import io 3 | from rest_framework.parsers import JSONParser 4 | from api.serializers import StudentSerializer 5 | from rest_framework.renderers import JSONRenderer 6 | from django.http import HttpResponse 7 | from django.views.decorators.csrf import csrf_exempt 8 | 9 | # Create your views here. 10 | @csrf_exempt 11 | def student_create(request): 12 | if request.method == 'POST': 13 | json_data = request.body 14 | stream = io.BytesIO(json_data) 15 | python_data = JSONParser().parse(stream) 16 | serializer = StudentSerializer(data = python_data) 17 | 18 | if serializer.is_valid(): 19 | # serializer.save() 20 | res = {'msg': 'Data Created'} 21 | json_data = JSONRenderer().render(res) 22 | return HttpResponse(json_data, content_type = 'application/json') 23 | 24 | json_data = JSONRenderer().render(serializer.errors) 25 | return HttpResponse(json_data, content_type = 'application/json') 26 | -------------------------------------------------------------------------------- /deserialize/db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/deserialize/db.sqlite3 -------------------------------------------------------------------------------- /deserialize/deserialize/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/deserialize/deserialize/__init__.py -------------------------------------------------------------------------------- /deserialize/deserialize/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/deserialize/deserialize/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /deserialize/deserialize/__pycache__/settings.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/deserialize/deserialize/__pycache__/settings.cpython-310.pyc -------------------------------------------------------------------------------- /deserialize/deserialize/__pycache__/urls.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/deserialize/deserialize/__pycache__/urls.cpython-310.pyc -------------------------------------------------------------------------------- /deserialize/deserialize/__pycache__/wsgi.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/deserialize/deserialize/__pycache__/wsgi.cpython-310.pyc -------------------------------------------------------------------------------- /deserialize/deserialize/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for deserialize project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'deserialize.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /deserialize/deserialize/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for deserialize project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.1.5. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'django-insecure-6*@n9$aw53!=eb_ie@o71i3q=8lozuk(d#97$m7udtbvb@((h&' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'rest_framework', 41 | 'api', 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'deserialize.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'deserialize.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/4.1/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': BASE_DIR / 'db.sqlite3', 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/4.1/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'UTC' 111 | 112 | USE_I18N = True 113 | 114 | USE_TZ = True 115 | 116 | 117 | # Static files (CSS, JavaScript, Images) 118 | # https://docs.djangoproject.com/en/4.1/howto/static-files/ 119 | 120 | STATIC_URL = 'static/' 121 | 122 | # Default primary key field type 123 | # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field 124 | 125 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 126 | -------------------------------------------------------------------------------- /deserialize/deserialize/urls.py: -------------------------------------------------------------------------------- 1 | """deserialize URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/4.1/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path 18 | from api import views 19 | 20 | urlpatterns = [ 21 | path('admin/', admin.site.urls), 22 | path('stucreate/', views.student_create) 23 | ] 24 | -------------------------------------------------------------------------------- /deserialize/deserialize/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for deserialize project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'deserialize.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /deserialize/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'deserialize.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /deserialize/myapp.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import json 3 | 4 | URL = "http://127.0.0.1:8000/stucreate/" 5 | 6 | data = { 7 | 'name': 'sonam', 8 | 'roll':'101', 9 | 'city': 'rachi', 10 | } 11 | 12 | json_data = json.dumps(data) 13 | 14 | r = requests.post(url= URL, data=json_data) 15 | 16 | data = r.json() 17 | print(data) -------------------------------------------------------------------------------- /function_based_crud/api/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/function_based_crud/api/__init__.py -------------------------------------------------------------------------------- /function_based_crud/api/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/function_based_crud/api/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /function_based_crud/api/__pycache__/admin.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/function_based_crud/api/__pycache__/admin.cpython-310.pyc -------------------------------------------------------------------------------- /function_based_crud/api/__pycache__/apps.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/function_based_crud/api/__pycache__/apps.cpython-310.pyc -------------------------------------------------------------------------------- /function_based_crud/api/__pycache__/models.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/function_based_crud/api/__pycache__/models.cpython-310.pyc -------------------------------------------------------------------------------- /function_based_crud/api/__pycache__/serializer.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/function_based_crud/api/__pycache__/serializer.cpython-310.pyc -------------------------------------------------------------------------------- /function_based_crud/api/__pycache__/views.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/function_based_crud/api/__pycache__/views.cpython-310.pyc -------------------------------------------------------------------------------- /function_based_crud/api/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from api.models import Student 3 | # Register your models here. 4 | 5 | @admin.register(Student) 6 | class StudentAdmin(admin.ModelAdmin): 7 | 8 | list_display = ['id', 'name', 'roll', 'city'] -------------------------------------------------------------------------------- /function_based_crud/api/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class ApiConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'api' 7 | -------------------------------------------------------------------------------- /function_based_crud/api/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.5 on 2023-01-31 07:24 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Student', 16 | fields=[ 17 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('name', models.CharField(max_length=50)), 19 | ('roll', models.IntegerField()), 20 | ('city', models.CharField(max_length=50)), 21 | ], 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /function_based_crud/api/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/function_based_crud/api/migrations/__init__.py -------------------------------------------------------------------------------- /function_based_crud/api/migrations/__pycache__/0001_initial.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/function_based_crud/api/migrations/__pycache__/0001_initial.cpython-310.pyc -------------------------------------------------------------------------------- /function_based_crud/api/migrations/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/function_based_crud/api/migrations/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /function_based_crud/api/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | 5 | class Student(models.Model): 6 | 7 | name = models.CharField(max_length=50) 8 | roll = models.IntegerField() 9 | city = models.CharField(max_length=50) -------------------------------------------------------------------------------- /function_based_crud/api/serializer.py: -------------------------------------------------------------------------------- 1 | from api.models import Student 2 | from rest_framework import serializers 3 | 4 | class StudentSerializer(serializers.ModelSerializer): 5 | 6 | class Meta: 7 | 8 | model = Student 9 | fields = ['id', 'name', 'roll', 'city'] -------------------------------------------------------------------------------- /function_based_crud/api/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /function_based_crud/api/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | from rest_framework.decorators import api_view 3 | from api.models import Student 4 | from rest_framework.response import Response 5 | from api.serializer import StudentSerializer 6 | from rest_framework import status 7 | # Create your views here. 8 | 9 | @api_view(['GET', 'POST', 'PUT', 'PATCH', 'DELETE']) 10 | def student_api(request, pk=None): 11 | 12 | if request.method == 'GET': 13 | id = pk 14 | 15 | if id is not None: 16 | stu = Student.objects.get(id=id) 17 | serializer = StudentSerializer(stu) 18 | return Response(serializer.data) 19 | 20 | stu = Student.objects.all() 21 | serializer = StudentSerializer(stu, many= True) 22 | return Response(serializer.data) 23 | 24 | if request.method == 'POST': 25 | serializer = StudentSerializer(data = request.data) 26 | if serializer.is_valid(): 27 | serializer.save() 28 | return Response ({'msg':'Data Created'}, status=status.HTTP_201_CREATED) 29 | return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) 30 | 31 | if request.method == 'PUT': 32 | id = pk 33 | stu = Student.objects.get(pk=id) 34 | serializer = StudentSerializer(stu, data= request.data) 35 | if serializer.is_valid(): 36 | serializer.save() 37 | return Response({'msg':'Data Updated'}, status=status.HTTP_200_OK) 38 | return Response(serializer.errors) 39 | 40 | if request.method == 'PATCH': 41 | id = pk 42 | stu = Student.objects.get(pk=id) 43 | serializer = StudentSerializer(stu, data= request.data, partial=True) 44 | if serializer.is_valid(): 45 | serializer.save() 46 | return Response({'msg':'Data Updated'}) 47 | return Response(serializer.errors) 48 | 49 | if request.method == 'DELETE': 50 | id = pk 51 | stu = Student.objects.get(pk=id) 52 | stu.delete() 53 | return Response({'msg': 'Data Deleted'}) -------------------------------------------------------------------------------- /function_based_crud/db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/function_based_crud/db.sqlite3 -------------------------------------------------------------------------------- /function_based_crud/function_based_crud/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/function_based_crud/function_based_crud/__init__.py -------------------------------------------------------------------------------- /function_based_crud/function_based_crud/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/function_based_crud/function_based_crud/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /function_based_crud/function_based_crud/__pycache__/settings.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/function_based_crud/function_based_crud/__pycache__/settings.cpython-310.pyc -------------------------------------------------------------------------------- /function_based_crud/function_based_crud/__pycache__/urls.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/function_based_crud/function_based_crud/__pycache__/urls.cpython-310.pyc -------------------------------------------------------------------------------- /function_based_crud/function_based_crud/__pycache__/wsgi.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/function_based_crud/function_based_crud/__pycache__/wsgi.cpython-310.pyc -------------------------------------------------------------------------------- /function_based_crud/function_based_crud/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for function_based_crud project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'function_based_crud.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /function_based_crud/function_based_crud/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for function_based_crud project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.1.5. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'django-insecure-!zr0w88fq#wc$p2=e%%egggoazrt3$8e^_ttc39!e(+bk-ue_d' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'rest_framework', 41 | 'api' 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'function_based_crud.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'function_based_crud.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/4.1/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': BASE_DIR / 'db.sqlite3', 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/4.1/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'UTC' 111 | 112 | USE_I18N = True 113 | 114 | USE_TZ = True 115 | 116 | 117 | # Static files (CSS, JavaScript, Images) 118 | # https://docs.djangoproject.com/en/4.1/howto/static-files/ 119 | 120 | STATIC_URL = 'static/' 121 | 122 | # Default primary key field type 123 | # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field 124 | 125 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 126 | -------------------------------------------------------------------------------- /function_based_crud/function_based_crud/urls.py: -------------------------------------------------------------------------------- 1 | """function_based_crud URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/4.1/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path 18 | from api import views 19 | urlpatterns = [ 20 | path('admin/', admin.site.urls), 21 | path('student_api', views.student_api), 22 | path('student_api/', views.student_api) 23 | ] 24 | -------------------------------------------------------------------------------- /function_based_crud/function_based_crud/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for function_based_crud project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'function_based_crud.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /function_based_crud/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'function_based_crud.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /function_based_crud/myapp.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import json 3 | 4 | URL = 'http://127.0.0.1:8000/student_api' 5 | 6 | def get_data(id = None): 7 | data = {} 8 | 9 | if id is not None: 10 | data = {'id': id} 11 | 12 | headers = {'content-Type': 'application/json'} 13 | json_data = json.dumps(data) 14 | r = requests.get(url= URL, headers= headers, data= json_data) 15 | data = r.json() 16 | print(data) 17 | 18 | get_data() 19 | 20 | def post_data(): 21 | data = { 22 | 'name': 'rohan', 23 | 'roll':'120', 24 | 'city':'Surat' 25 | } 26 | 27 | headers = {'content-Type': 'application/json'} 28 | json_data = json.dumps(data) 29 | r = requests.post(url= URL, headers=headers, data= json_data) 30 | data= r.json() 31 | print(data) 32 | 33 | # post_data() 34 | 35 | def update_data(): 36 | data = { 37 | 'id':3, 38 | 'name': 'Rohan', 39 | 'city':'Ahmadabad', 40 | 'roll': '104' 41 | } 42 | 43 | headers = {'content-Type': 'application/json'} 44 | json_data = json.dumps(data) 45 | r = requests.put(url= URL, headers=headers, data= json_data) 46 | data= r.json() 47 | print(data) 48 | 49 | # update_data() 50 | 51 | def delete_data(): 52 | data = { 53 | 'id': 3 54 | } 55 | 56 | headers = {'content-Type': 'application/json'} 57 | json_data = json.dumps(data) 58 | r = requests.delete(url= URL, headers=headers, data= json_data) 59 | data = r.json() 60 | print(data) 61 | 62 | # delete_data() -------------------------------------------------------------------------------- /generic_api/api_view/api/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/api_view/api/__init__.py -------------------------------------------------------------------------------- /generic_api/api_view/api/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/api_view/api/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/api_view/api/__pycache__/admin.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/api_view/api/__pycache__/admin.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/api_view/api/__pycache__/apps.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/api_view/api/__pycache__/apps.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/api_view/api/__pycache__/models.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/api_view/api/__pycache__/models.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/api_view/api/__pycache__/serializer.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/api_view/api/__pycache__/serializer.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/api_view/api/__pycache__/views.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/api_view/api/__pycache__/views.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/api_view/api/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from api.models import Student 3 | 4 | # Register your models here. 5 | 6 | @admin.register(Student) 7 | class StudentAdmin(admin.ModelAdmin): 8 | 9 | list_display = ['id', 'name', 'roll', 'city'] 10 | -------------------------------------------------------------------------------- /generic_api/api_view/api/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class ApiConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'api' 7 | -------------------------------------------------------------------------------- /generic_api/api_view/api/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.5 on 2023-02-01 06:31 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Student', 16 | fields=[ 17 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('name', models.CharField(max_length=50)), 19 | ('roll', models.IntegerField()), 20 | ('city', models.CharField(max_length=50)), 21 | ], 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /generic_api/api_view/api/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/api_view/api/migrations/__init__.py -------------------------------------------------------------------------------- /generic_api/api_view/api/migrations/__pycache__/0001_initial.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/api_view/api/migrations/__pycache__/0001_initial.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/api_view/api/migrations/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/api_view/api/migrations/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/api_view/api/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | 5 | class Student(models.Model): 6 | 7 | name = models.CharField(max_length=50) 8 | roll = models.IntegerField() 9 | city = models.CharField(max_length=50) -------------------------------------------------------------------------------- /generic_api/api_view/api/serializer.py: -------------------------------------------------------------------------------- 1 | from api.models import Student 2 | from rest_framework import serializers 3 | 4 | class StudentSerializer(serializers.ModelSerializer): 5 | 6 | class Meta: 7 | 8 | model = Student 9 | fields = ['id', 'name', 'roll', 'city'] 10 | -------------------------------------------------------------------------------- /generic_api/api_view/api/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /generic_api/api_view/api/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | from rest_framework.generics import (ListAPIView, CreateAPIView, RetrieveAPIView, UpdateAPIView, DestroyAPIView, 3 | ListCreateAPIView, RetrieveUpdateAPIView, RetrieveDestroyAPIView, 4 | RetrieveUpdateDestroyAPIView) 5 | from api.models import Student 6 | from api.serializer import StudentSerializer 7 | 8 | # Create your views here. 9 | 10 | class LCStudent(ListAPIView, CreateAPIView): 11 | 12 | queryset = Student.objects.all() 13 | serializer_class = StudentSerializer 14 | 15 | class RUDStudent(RetrieveAPIView, UpdateAPIView, DestroyAPIView): 16 | 17 | queryset = Student.objects.all() 18 | serializer_class = StudentSerializer -------------------------------------------------------------------------------- /generic_api/api_view/api_view/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/api_view/api_view/__init__.py -------------------------------------------------------------------------------- /generic_api/api_view/api_view/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/api_view/api_view/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/api_view/api_view/__pycache__/settings.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/api_view/api_view/__pycache__/settings.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/api_view/api_view/__pycache__/urls.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/api_view/api_view/__pycache__/urls.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/api_view/api_view/__pycache__/wsgi.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/api_view/api_view/__pycache__/wsgi.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/api_view/api_view/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for api_view project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api_view.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /generic_api/api_view/api_view/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for api_view project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.1.5. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'django-insecure-)5z&7=d75khm)+$5^fv=b=w*5v2d&2vb5+jz8e^!dp%_b+n*uu' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'rest_framework', 41 | 'api' 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'api_view.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'api_view.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/4.1/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': BASE_DIR / 'db.sqlite3', 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/4.1/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'UTC' 111 | 112 | USE_I18N = True 113 | 114 | USE_TZ = True 115 | 116 | 117 | # Static files (CSS, JavaScript, Images) 118 | # https://docs.djangoproject.com/en/4.1/howto/static-files/ 119 | 120 | STATIC_URL = 'static/' 121 | 122 | # Default primary key field type 123 | # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field 124 | 125 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 126 | -------------------------------------------------------------------------------- /generic_api/api_view/api_view/urls.py: -------------------------------------------------------------------------------- 1 | """api_view URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/4.1/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path 18 | from api import views 19 | 20 | urlpatterns = [ 21 | path('admin/', admin.site.urls), 22 | path('student/', views.LCStudent.as_view()), 23 | path('student/', views.RUDStudent.as_view()) 24 | ] 25 | -------------------------------------------------------------------------------- /generic_api/api_view/api_view/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for api_view project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api_view.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /generic_api/api_view/db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/api_view/db.sqlite3 -------------------------------------------------------------------------------- /generic_api/api_view/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api_view.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /generic_api/mixin/api/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/mixin/api/__init__.py -------------------------------------------------------------------------------- /generic_api/mixin/api/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/mixin/api/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/mixin/api/__pycache__/admin.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/mixin/api/__pycache__/admin.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/mixin/api/__pycache__/apps.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/mixin/api/__pycache__/apps.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/mixin/api/__pycache__/models.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/mixin/api/__pycache__/models.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/mixin/api/__pycache__/serializer.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/mixin/api/__pycache__/serializer.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/mixin/api/__pycache__/views.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/mixin/api/__pycache__/views.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/mixin/api/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from api.models import Student 3 | # Register your models here. 4 | 5 | @admin.register(Student) 6 | class StudentAdmin(admin.ModelAdmin): 7 | 8 | list_display = ['id', 'name', 'roll', 'city'] -------------------------------------------------------------------------------- /generic_api/mixin/api/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class ApiConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'api' 7 | -------------------------------------------------------------------------------- /generic_api/mixin/api/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.5 on 2023-01-31 07:24 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Student', 16 | fields=[ 17 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('name', models.CharField(max_length=50)), 19 | ('roll', models.IntegerField()), 20 | ('city', models.CharField(max_length=50)), 21 | ], 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /generic_api/mixin/api/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/mixin/api/migrations/__init__.py -------------------------------------------------------------------------------- /generic_api/mixin/api/migrations/__pycache__/0001_initial.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/mixin/api/migrations/__pycache__/0001_initial.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/mixin/api/migrations/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/mixin/api/migrations/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/mixin/api/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | 5 | class Student(models.Model): 6 | 7 | name = models.CharField(max_length=50) 8 | roll = models.IntegerField() 9 | city = models.CharField(max_length=50) -------------------------------------------------------------------------------- /generic_api/mixin/api/serializer.py: -------------------------------------------------------------------------------- 1 | from api.models import Student 2 | from rest_framework import serializers 3 | 4 | class StudentSerializer(serializers.ModelSerializer): 5 | 6 | class Meta: 7 | 8 | model = Student 9 | fields = ['id', 'name', 'roll', 'city'] -------------------------------------------------------------------------------- /generic_api/mixin/api/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /generic_api/mixin/api/views.py: -------------------------------------------------------------------------------- 1 | from api.models import Student 2 | from api.serializer import StudentSerializer 3 | from rest_framework.generics import GenericAPIView 4 | from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin 5 | 6 | class LCStudent(GenericAPIView, ListModelMixin, CreateModelMixin): 7 | queryset = Student.objects.all() 8 | serializer_class = StudentSerializer 9 | 10 | """function for get all data""" 11 | def get(self, request, *args, **kwargs): 12 | return self.list(request, *args, **kwargs) 13 | 14 | 15 | """function for create data""" 16 | def post(self, request, *args, **kwargs): 17 | return self.create(request, *args, **kwargs) 18 | 19 | class RUDStudent(GenericAPIView, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin): 20 | 21 | queryset = Student.objects.all() 22 | serializer_class = StudentSerializer 23 | 24 | def get(self, request, *args, **kwargs): 25 | return self.retrieve(request, *args, **kwargs) 26 | 27 | def put(self, request, *args, **kwargs): 28 | return self.update(request, *args, **kwargs) 29 | 30 | def delete(self, request, *args, **kwargs): 31 | return self.destroy(request, *args, **kwargs) -------------------------------------------------------------------------------- /generic_api/mixin/db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/mixin/db.sqlite3 -------------------------------------------------------------------------------- /generic_api/mixin/generic_api/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/mixin/generic_api/__init__.py -------------------------------------------------------------------------------- /generic_api/mixin/generic_api/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/mixin/generic_api/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/mixin/generic_api/__pycache__/settings.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/mixin/generic_api/__pycache__/settings.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/mixin/generic_api/__pycache__/urls.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/mixin/generic_api/__pycache__/urls.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/mixin/generic_api/__pycache__/wsgi.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/generic_api/mixin/generic_api/__pycache__/wsgi.cpython-310.pyc -------------------------------------------------------------------------------- /generic_api/mixin/generic_api/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for generic_api project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'generic_api.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /generic_api/mixin/generic_api/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for generic_api project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.1.5. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'django-insecure-q!ouy496lg5)x_ddaaaimj5xd4p476+dtirm_0uh!pjx1vp&xo' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'rest_framework', 41 | 'api' 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'generic_api.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'generic_api.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/4.1/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': BASE_DIR / 'db.sqlite3', 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/4.1/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'UTC' 111 | 112 | USE_I18N = True 113 | 114 | USE_TZ = True 115 | 116 | 117 | # Static files (CSS, JavaScript, Images) 118 | # https://docs.djangoproject.com/en/4.1/howto/static-files/ 119 | 120 | STATIC_URL = 'static/' 121 | 122 | # Default primary key field type 123 | # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field 124 | 125 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 126 | -------------------------------------------------------------------------------- /generic_api/mixin/generic_api/urls.py: -------------------------------------------------------------------------------- 1 | """generic_api URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/4.1/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path 18 | from api import views 19 | urlpatterns = [ 20 | path('admin/', admin.site.urls), 21 | path('student/', views.LCStudent.as_view()), 22 | path('student/', views.RUDStudent.as_view()), 23 | ] -------------------------------------------------------------------------------- /generic_api/mixin/generic_api/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for generic_api project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'generic_api.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /generic_api/mixin/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'generic_api.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /generic_api/mixin/myapp.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import json 3 | 4 | URL = 'http://127.0.0.1:8000/student_api' 5 | 6 | def get_data(id = None): 7 | data = {} 8 | 9 | if id is not None: 10 | data = {'id': id} 11 | 12 | headers = {'content-Type': 'application/json'} 13 | json_data = json.dumps(data) 14 | r = requests.get(url= URL, headers= headers, data= json_data) 15 | data = r.json() 16 | print(data) 17 | 18 | get_data() 19 | 20 | def post_data(): 21 | data = { 22 | 'name': 'rohan', 23 | 'roll':'120', 24 | 'city':'Surat' 25 | } 26 | 27 | headers = {'content-Type': 'application/json'} 28 | json_data = json.dumps(data) 29 | r = requests.post(url= URL, headers=headers, data= json_data) 30 | data= r.json() 31 | print(data) 32 | 33 | # post_data() 34 | 35 | def update_data(): 36 | data = { 37 | 'id':3, 38 | 'name': 'Rohan', 39 | 'city':'Ahmadabad', 40 | 'roll': '104' 41 | } 42 | 43 | headers = {'content-Type': 'application/json'} 44 | json_data = json.dumps(data) 45 | r = requests.put(url= URL, headers=headers, data= json_data) 46 | data= r.json() 47 | print(data) 48 | 49 | # update_data() 50 | 51 | def delete_data(): 52 | data = { 53 | 'id': 3 54 | } 55 | 56 | headers = {'content-Type': 'application/json'} 57 | json_data = json.dumps(data) 58 | r = requests.delete(url= URL, headers=headers, data= json_data) 59 | data = r.json() 60 | print(data) 61 | 62 | # delete_data() -------------------------------------------------------------------------------- /model_serializer/api/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/model_serializer/api/__init__.py -------------------------------------------------------------------------------- /model_serializer/api/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/model_serializer/api/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /model_serializer/api/__pycache__/admin.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/model_serializer/api/__pycache__/admin.cpython-310.pyc -------------------------------------------------------------------------------- /model_serializer/api/__pycache__/apps.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/model_serializer/api/__pycache__/apps.cpython-310.pyc -------------------------------------------------------------------------------- /model_serializer/api/__pycache__/models.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/model_serializer/api/__pycache__/models.cpython-310.pyc -------------------------------------------------------------------------------- /model_serializer/api/__pycache__/serializers.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/model_serializer/api/__pycache__/serializers.cpython-310.pyc -------------------------------------------------------------------------------- /model_serializer/api/__pycache__/views.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/model_serializer/api/__pycache__/views.cpython-310.pyc -------------------------------------------------------------------------------- /model_serializer/api/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from api.models import Student 3 | # Register your models here. 4 | 5 | @admin.register(Student) 6 | class StudentAdmin(admin.ModelAdmin): 7 | 8 | list_display = ['id', 'name', 'roll', 'city'] -------------------------------------------------------------------------------- /model_serializer/api/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class ApiConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'api' 7 | -------------------------------------------------------------------------------- /model_serializer/api/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.5 on 2023-01-30 12:12 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Student', 16 | fields=[ 17 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('name', models.CharField(max_length=50)), 19 | ('roll', models.IntegerField()), 20 | ('city', models.CharField(max_length=50)), 21 | ], 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /model_serializer/api/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/model_serializer/api/migrations/__init__.py -------------------------------------------------------------------------------- /model_serializer/api/migrations/__pycache__/0001_initial.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/model_serializer/api/migrations/__pycache__/0001_initial.cpython-310.pyc -------------------------------------------------------------------------------- /model_serializer/api/migrations/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/model_serializer/api/migrations/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /model_serializer/api/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | 5 | class Student(models.Model): 6 | 7 | name = models.CharField(max_length=50) 8 | roll = models.IntegerField() 9 | city = models.CharField(max_length=50) -------------------------------------------------------------------------------- /model_serializer/api/serializers.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | from api.models import Student 3 | 4 | class StudentSerializer(serializers.ModelSerializer): 5 | class Meta: 6 | 7 | model = Student 8 | fields = ['id', 'name', 'roll', 'city'] 9 | 10 | # read_only_fields & extra_kwarg is use to no changes mention fields in update method 11 | read_only_fields = ['name', 'roll'] 12 | extra_kwargs = {'name':{'read_only': True}} -------------------------------------------------------------------------------- /model_serializer/api/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /model_serializer/api/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | import io 3 | from rest_framework.parsers import JSONParser 4 | from api.models import Student 5 | from api.serializers import StudentSerializer 6 | from rest_framework.renderers import JSONRenderer 7 | from django.http import HttpResponse 8 | from django.views.decorators.csrf import csrf_exempt 9 | from django.utils.decorators import method_decorator 10 | from django.views import View 11 | 12 | # Create your views here. 13 | @method_decorator(csrf_exempt, name='dispatch') 14 | class StudentAPI(View): 15 | def get(self, request, *args, **kwargs): 16 | json_data = request.body 17 | stream = io.BytesIO(json_data) 18 | python_data = JSONParser().parse(stream) 19 | id = python_data.get('id', None) 20 | 21 | if id is None: 22 | stu = Student.objects.get(id = id) 23 | serializer = StudentSerializer(stu) 24 | json_data = JSONRenderer().render(serializer.data) 25 | return HttpResponse(json_data, content_type = 'application/json') 26 | 27 | stu = Student.objects.all() 28 | serializer = StudentSerializer(stu, many = True) 29 | json_data = JSONRenderer().render(serializer.data) 30 | return HttpResponse(json_data, content_type = 'application/json') 31 | 32 | def post(self, request, *args, **kwargs): 33 | 34 | json_data = request.body 35 | stream = io.BytesIO(json_data) 36 | python_data = JSONParser().parse(stream) 37 | serializer = StudentSerializer(data= python_data) 38 | 39 | if serializer.is_valid(): 40 | serializer.save() 41 | res = {"msg":"data created"} 42 | json_data = JSONRenderer().render(res) 43 | return HttpResponse(json_data, content_type = '/application/json') 44 | 45 | json_data = JSONRenderer().render(serializer.errors) 46 | return HttpResponse(json_data, content_type = '/application/json') 47 | 48 | def put(self, request, *args, **kwargs): 49 | 50 | json_data = request.body 51 | stream = io.BytesIO(json_data) 52 | python_data = JSONParser().parse(stream) 53 | id=python_data.get('id') 54 | stu = Student.objects.get(id=id) 55 | serializer = StudentSerializer(stu, data= python_data, partial = True) 56 | 57 | if serializer.is_valid(): 58 | serializer.save() 59 | res = {"msg": "data updated"} 60 | json_data = JSONRenderer().render(res) 61 | return HttpResponse(json_data, content_type = 'application/json') 62 | 63 | json_data = JSONRenderer().render(res) 64 | return HttpResponse(json_data, content_type = 'application/json') 65 | 66 | def delete(self, request, *args, **kwargs): 67 | 68 | json_data = request.body 69 | stream = io.BytesIO(json_data) 70 | python_data = JSONParser().parse(stream) 71 | id = python_data.get('id') 72 | stu = Student.objects.get(id = id) 73 | stu.delete() 74 | res = {"mes": "data deleted"} 75 | json_data = JSONRenderer().render(res) 76 | return HttpResponse(json_data, content_type = 'application/json') 77 | -------------------------------------------------------------------------------- /model_serializer/db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/model_serializer/db.sqlite3 -------------------------------------------------------------------------------- /model_serializer/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'model_serializer.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /model_serializer/model_serializer/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/model_serializer/model_serializer/__init__.py -------------------------------------------------------------------------------- /model_serializer/model_serializer/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/model_serializer/model_serializer/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /model_serializer/model_serializer/__pycache__/settings.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/model_serializer/model_serializer/__pycache__/settings.cpython-310.pyc -------------------------------------------------------------------------------- /model_serializer/model_serializer/__pycache__/urls.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/model_serializer/model_serializer/__pycache__/urls.cpython-310.pyc -------------------------------------------------------------------------------- /model_serializer/model_serializer/__pycache__/wsgi.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/model_serializer/model_serializer/__pycache__/wsgi.cpython-310.pyc -------------------------------------------------------------------------------- /model_serializer/model_serializer/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for model_serializer project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'model_serializer.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /model_serializer/model_serializer/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for model_serializer project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.1.5. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'django-insecure-=+(c6$lqyskmj=2!_u#et5mx2qap24pqe$v0ipxj!v8zfz_v0=' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'rest_framework', 41 | 'api' 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'model_serializer.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'model_serializer.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/4.1/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': BASE_DIR / 'db.sqlite3', 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/4.1/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'UTC' 111 | 112 | USE_I18N = True 113 | 114 | USE_TZ = True 115 | 116 | 117 | # Static files (CSS, JavaScript, Images) 118 | # https://docs.djangoproject.com/en/4.1/howto/static-files/ 119 | 120 | STATIC_URL = 'static/' 121 | 122 | # Default primary key field type 123 | # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field 124 | 125 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 126 | -------------------------------------------------------------------------------- /model_serializer/model_serializer/urls.py: -------------------------------------------------------------------------------- 1 | """model_serializer URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/4.1/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path 18 | from api import views 19 | 20 | urlpatterns = [ 21 | path('admin/', admin.site.urls), 22 | path('student_api', views.StudentAPI.as_view()) 23 | ] 24 | 25 | -------------------------------------------------------------------------------- /model_serializer/model_serializer/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for model_serializer project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'model_serializer.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /model_serializer/myapp.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import json 3 | 4 | URL = 'http://127.0.0.1:8000/student_api' 5 | 6 | def get_data(id = None): 7 | data = {} 8 | 9 | if id is not None: 10 | data = {'id': id} 11 | 12 | json_data = json.dumps(data) 13 | r = requests.get(url= URL, data= json_data) 14 | data = r.json() 15 | print(data) 16 | 17 | # get_data(1) 18 | 19 | def post_data(): 20 | data = { 21 | 'name': 'ravi', 22 | 'roll':'104', 23 | 'city':'Dhanbad' 24 | } 25 | 26 | json_data = json.dumps(data) 27 | r = requests.post(url= URL, data= json_data) 28 | data= r.json() 29 | print(data) 30 | 31 | # post_data() 32 | 33 | def update_data(): 34 | data = { 35 | 'id':4, 36 | 'name': 'Rohit', 37 | 'city':'Surat', 38 | 'roll': '104' 39 | } 40 | 41 | json_data = json.dumps(data) 42 | r = requests.put(url= URL, data= json_data) 43 | data= r.json() 44 | print(data) 45 | 46 | update_data() 47 | 48 | def delete_data(): 49 | data = { 50 | 'id': 4 51 | } 52 | 53 | json_data = json.dumps(data) 54 | r = requests.delete(url= URL, data= json_data) 55 | data = r.json() 56 | print(data) 57 | 58 | # delete_data() -------------------------------------------------------------------------------- /serialize/api/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/serialize/api/__init__.py -------------------------------------------------------------------------------- /serialize/api/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/serialize/api/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /serialize/api/__pycache__/admin.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/serialize/api/__pycache__/admin.cpython-310.pyc -------------------------------------------------------------------------------- /serialize/api/__pycache__/apps.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/serialize/api/__pycache__/apps.cpython-310.pyc -------------------------------------------------------------------------------- /serialize/api/__pycache__/models.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/serialize/api/__pycache__/models.cpython-310.pyc -------------------------------------------------------------------------------- /serialize/api/__pycache__/serializer.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/serialize/api/__pycache__/serializer.cpython-310.pyc -------------------------------------------------------------------------------- /serialize/api/__pycache__/views.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/serialize/api/__pycache__/views.cpython-310.pyc -------------------------------------------------------------------------------- /serialize/api/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from api.models import Student 3 | # Register your models here. 4 | 5 | @admin.register(Student) 6 | class StudentAdmin(admin.ModelAdmin): 7 | list_display = ['id', 'name', 'roll', 'city'] -------------------------------------------------------------------------------- /serialize/api/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class ApiConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'api' 7 | -------------------------------------------------------------------------------- /serialize/api/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.3 on 2023-01-29 15:47 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Student', 16 | fields=[ 17 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('name', models.CharField(max_length=100)), 19 | ('roll', models.IntegerField()), 20 | ('city', models.CharField(max_length=100)), 21 | ], 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /serialize/api/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/serialize/api/migrations/__init__.py -------------------------------------------------------------------------------- /serialize/api/migrations/__pycache__/0001_initial.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/serialize/api/migrations/__pycache__/0001_initial.cpython-310.pyc -------------------------------------------------------------------------------- /serialize/api/migrations/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/serialize/api/migrations/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /serialize/api/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | 5 | class Student(models.Model): 6 | name = models.CharField(max_length=100) 7 | roll = models.IntegerField() 8 | city = models.CharField(max_length=100) -------------------------------------------------------------------------------- /serialize/api/serializer.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | 3 | class StudentSerializer(serializers.Serializer): 4 | 5 | id = serializers.IntegerField() 6 | name = serializers.CharField(max_length=100) 7 | roll = serializers.IntegerField() 8 | city = serializers.CharField(max_length=100) 9 | 10 | -------------------------------------------------------------------------------- /serialize/api/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /serialize/api/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | from api.models import Student 3 | from api.serializer import StudentSerializer 4 | from rest_framework.renderers import JSONRenderer 5 | from django.http import HttpResponse, JsonResponse 6 | 7 | # Create your views here. 8 | 9 | def student_detail(request, pk): 10 | stu = Student.objects.get(id = pk) 11 | serializer = StudentSerializer(stu) 12 | # json_data = JSONRenderer().render(serializer.data) 13 | # return HttpResponse(json_data, content_type = 'application/json') 14 | return JsonResponse(serializer.data) 15 | 16 | # All student data 17 | def student_list(request): 18 | stu = Student.objects.all() 19 | serializer = StudentSerializer(stu, many = True) 20 | # json_data = JSONRenderer().render(serializer.data) 21 | # return HttpResponse(json_data, content_type = 'application/json') 22 | return JsonResponse(serializer.data, safe= False) -------------------------------------------------------------------------------- /serialize/db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/serialize/db.sqlite3 -------------------------------------------------------------------------------- /serialize/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'serialize.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /serialize/serialize/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/serialize/serialize/__init__.py -------------------------------------------------------------------------------- /serialize/serialize/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/serialize/serialize/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /serialize/serialize/__pycache__/settings.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/serialize/serialize/__pycache__/settings.cpython-310.pyc -------------------------------------------------------------------------------- /serialize/serialize/__pycache__/urls.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/serialize/serialize/__pycache__/urls.cpython-310.pyc -------------------------------------------------------------------------------- /serialize/serialize/__pycache__/wsgi.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/serialize/serialize/__pycache__/wsgi.cpython-310.pyc -------------------------------------------------------------------------------- /serialize/serialize/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for serialize project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'serialize.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /serialize/serialize/myapp.py: -------------------------------------------------------------------------------- 1 | import requests 2 | 3 | URL = 'http://127.0.0.1:8000/stuinfo/2' 4 | 5 | r = requests.get(url=URL) 6 | 7 | data = r.json() 8 | 9 | print() -------------------------------------------------------------------------------- /serialize/serialize/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for serialize project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.1.3. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'django-insecure-d!k!30cnr8^n5j4s!tc6%v5m%mg8(27k8o(#p&h-m$f-!x&3b(' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'rest_framework', 41 | 'api' 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'serialize.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'serialize.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/4.1/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': BASE_DIR / 'db.sqlite3', 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/4.1/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'UTC' 111 | 112 | USE_I18N = True 113 | 114 | USE_TZ = True 115 | 116 | 117 | # Static files (CSS, JavaScript, Images) 118 | # https://docs.djangoproject.com/en/4.1/howto/static-files/ 119 | 120 | STATIC_URL = 'static/' 121 | 122 | # Default primary key field type 123 | # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field 124 | 125 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 126 | -------------------------------------------------------------------------------- /serialize/serialize/urls.py: -------------------------------------------------------------------------------- 1 | """serialize URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/4.1/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path 18 | from api import views 19 | urlpatterns = [ 20 | path('admin/', admin.site.urls), 21 | path('stuinfo/', views.student_detail), 22 | path('stuinfo/', views.student_list) 23 | ] 24 | -------------------------------------------------------------------------------- /serialize/serialize/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for serialize project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'serialize.settings') 15 | 16 | application = get_wsgi_application() -------------------------------------------------------------------------------- /validation/api/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/validation/api/__init__.py -------------------------------------------------------------------------------- /validation/api/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/validation/api/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /validation/api/__pycache__/admin.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/validation/api/__pycache__/admin.cpython-310.pyc -------------------------------------------------------------------------------- /validation/api/__pycache__/apps.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/validation/api/__pycache__/apps.cpython-310.pyc -------------------------------------------------------------------------------- /validation/api/__pycache__/models.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/validation/api/__pycache__/models.cpython-310.pyc -------------------------------------------------------------------------------- /validation/api/__pycache__/serializers.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/validation/api/__pycache__/serializers.cpython-310.pyc -------------------------------------------------------------------------------- /validation/api/__pycache__/views.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/validation/api/__pycache__/views.cpython-310.pyc -------------------------------------------------------------------------------- /validation/api/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from api.models import Student 3 | # Register your models here. 4 | 5 | @admin.register(Student) 6 | class StudentAdmin(admin.ModelAdmin): 7 | 8 | list_display = ['id', 'name', 'roll', 'city'] -------------------------------------------------------------------------------- /validation/api/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class ApiConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'api' 7 | -------------------------------------------------------------------------------- /validation/api/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.5 on 2023-01-30 12:12 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Student', 16 | fields=[ 17 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('name', models.CharField(max_length=50)), 19 | ('roll', models.IntegerField()), 20 | ('city', models.CharField(max_length=50)), 21 | ], 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /validation/api/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/validation/api/migrations/__init__.py -------------------------------------------------------------------------------- /validation/api/migrations/__pycache__/0001_initial.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/validation/api/migrations/__pycache__/0001_initial.cpython-310.pyc -------------------------------------------------------------------------------- /validation/api/migrations/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/validation/api/migrations/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /validation/api/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | 5 | class Student(models.Model): 6 | 7 | name = models.CharField(max_length=50) 8 | roll = models.IntegerField() 9 | city = models.CharField(max_length=50) -------------------------------------------------------------------------------- /validation/api/serializers.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | from api.models import Student 3 | 4 | # Validators 5 | 6 | def start_with_r(value): 7 | if value[0].lower() != 'r': 8 | raise serializers.ValidationError('Start with R') 9 | 10 | class StudentSerializer(serializers.Serializer): 11 | 12 | name = serializers.CharField(max_length=50, validators = [start_with_r]) 13 | roll = serializers.IntegerField() 14 | city = serializers.CharField(max_length=50) 15 | 16 | def create(self, validated_data): 17 | return Student.objects.create(**validated_data) 18 | 19 | def update(self, instance, validated_data): 20 | print(instance.name) 21 | instance.name = validated_data.get('name', instance.name) 22 | instance.roll = validated_data.get('roll', instance.roll) 23 | instance.city = validated_data.get('city', instance.city) 24 | instance.save() 25 | return instance 26 | 27 | # Felid level validation 28 | 29 | def validate_roll(self, value): 30 | if value >= 200: 31 | raise serializers.ValidationError('Seat Full') 32 | return value 33 | 34 | # object level validation 35 | 36 | def validate(self, data): 37 | nm = data.get('name') 38 | ct = data.get('city') 39 | 40 | if nm.lower() == 'rohit' and ct.lower() != 'surat': 41 | raise serializers.ValidationError('City must be Surat') 42 | return data -------------------------------------------------------------------------------- /validation/api/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /validation/api/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | import io 3 | from rest_framework.parsers import JSONParser 4 | from api.models import Student 5 | from api.serializers import StudentSerializer 6 | from rest_framework.renderers import JSONRenderer 7 | from django.http import HttpResponse 8 | from django.views.decorators.csrf import csrf_exempt 9 | from django.utils.decorators import method_decorator 10 | from django.views import View 11 | 12 | # Create your views here. 13 | @method_decorator(csrf_exempt, name='dispatch') 14 | class StudentAPI(View): 15 | def get(self, request, *args, **kwargs): 16 | json_data = request.body 17 | stream = io.BytesIO(json_data) 18 | python_data = JSONParser().parse(stream) 19 | id = python_data.get('id', None) 20 | 21 | if id is None: 22 | stu = Student.objects.get(id = id) 23 | serializer = StudentSerializer(stu) 24 | json_data = JSONRenderer().render(serializer.data) 25 | return HttpResponse(json_data, content_type = 'application/json') 26 | 27 | stu = Student.objects.all() 28 | serializer = StudentSerializer(stu, many = True) 29 | json_data = JSONRenderer().render(serializer.data) 30 | return HttpResponse(json_data, content_type = 'application/json') 31 | 32 | def post(self, request, *args, **kwargs): 33 | 34 | json_data = request.body 35 | stream = io.BytesIO(json_data) 36 | python_data = JSONParser().parse(stream) 37 | serializer = StudentSerializer(data= python_data) 38 | 39 | if serializer.is_valid(): 40 | serializer.save() 41 | res = {"msg":"data created"} 42 | json_data = JSONRenderer().render(res) 43 | return HttpResponse(json_data, content_type = '/application/json') 44 | 45 | json_data = JSONRenderer().render(serializer.errors) 46 | return HttpResponse(json_data, content_type = '/application/json') 47 | 48 | def put(self, request, *args, **kwargs): 49 | 50 | json_data = request.body 51 | stream = io.BytesIO(json_data) 52 | python_data = JSONParser().parse(stream) 53 | id=python_data.get('id') 54 | stu = Student.objects.get(id=id) 55 | serializer = StudentSerializer(stu, data= python_data, partial = True) 56 | 57 | if serializer.is_valid(): 58 | serializer.save() 59 | res = {"msg": "data updated"} 60 | json_data = JSONRenderer().render(res) 61 | return HttpResponse(json_data, content_type = 'application/json') 62 | 63 | json_data = JSONRenderer().render(res) 64 | return HttpResponse(json_data, content_type = 'application/json') 65 | 66 | def delete(self, request, *args, **kwargs): 67 | 68 | json_data = request.body 69 | stream = io.BytesIO(json_data) 70 | python_data = JSONParser().parse(stream) 71 | id = python_data.get('id') 72 | stu = Student.objects.get(id = id) 73 | stu.delete() 74 | res = {"mes": "data deleted"} 75 | json_data = JSONRenderer().render(res) 76 | return HttpResponse(json_data, content_type = 'application/json') 77 | -------------------------------------------------------------------------------- /validation/db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/validation/db.sqlite3 -------------------------------------------------------------------------------- /validation/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'validation.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /validation/myapp.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import json 3 | 4 | URL = 'http://127.0.0.1:8000/student_api' 5 | 6 | def get_data(id = None): 7 | data = {} 8 | 9 | if id is not None: 10 | data = {'id': id} 11 | 12 | json_data = json.dumps(data) 13 | r = requests.get(url= URL, data= json_data) 14 | data = r.json() 15 | print(data) 16 | 17 | # get_data(1) 18 | 19 | def post_data(): 20 | data = { 21 | 'name': 'rohan', 22 | 'roll':'120', 23 | 'city':'Surat' 24 | } 25 | 26 | json_data = json.dumps(data) 27 | r = requests.post(url= URL, data= json_data) 28 | data= r.json() 29 | print(data) 30 | 31 | post_data() 32 | 33 | def update_data(): 34 | data = { 35 | 'id':3, 36 | 'name': 'Rohan', 37 | 'city':'Ahmadabad', 38 | 'roll': '104' 39 | } 40 | 41 | json_data = json.dumps(data) 42 | r = requests.put(url= URL, data= json_data) 43 | data= r.json() 44 | print(data) 45 | 46 | # update_data() 47 | 48 | def delete_data(): 49 | data = { 50 | 'id': 4 51 | } 52 | 53 | json_data = json.dumps(data) 54 | r = requests.delete(url= URL, data= json_data) 55 | data = r.json() 56 | print(data) 57 | 58 | # delete_data() -------------------------------------------------------------------------------- /validation/validation/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/validation/validation/__init__.py -------------------------------------------------------------------------------- /validation/validation/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/validation/validation/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /validation/validation/__pycache__/settings.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/validation/validation/__pycache__/settings.cpython-310.pyc -------------------------------------------------------------------------------- /validation/validation/__pycache__/urls.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/validation/validation/__pycache__/urls.cpython-310.pyc -------------------------------------------------------------------------------- /validation/validation/__pycache__/wsgi.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/validation/validation/__pycache__/wsgi.cpython-310.pyc -------------------------------------------------------------------------------- /validation/validation/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for validation project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'validation.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /validation/validation/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for validation project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.1.5. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'django-insecure-&qfyvoe#3_gwl2sn(z!-4q1un+_bqum1t(j7(#7172@@$!p(ho' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'rest_framework', 41 | 'api' 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'validation.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'validation.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/4.1/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': BASE_DIR / 'db.sqlite3', 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/4.1/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'UTC' 111 | 112 | USE_I18N = True 113 | 114 | USE_TZ = True 115 | 116 | 117 | # Static files (CSS, JavaScript, Images) 118 | # https://docs.djangoproject.com/en/4.1/howto/static-files/ 119 | 120 | STATIC_URL = 'static/' 121 | 122 | # Default primary key field type 123 | # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field 124 | 125 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 126 | -------------------------------------------------------------------------------- /validation/validation/urls.py: -------------------------------------------------------------------------------- 1 | """validation URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/4.1/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path 18 | from api import views 19 | 20 | urlpatterns = [ 21 | path('admin/', admin.site.urls), 22 | path('student_api', views.StudentAPI.as_view()) 23 | ] 24 | -------------------------------------------------------------------------------- /validation/validation/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for validation project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'validation.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /view_set/api/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/view_set/api/__init__.py -------------------------------------------------------------------------------- /view_set/api/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/view_set/api/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /view_set/api/__pycache__/admin.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/view_set/api/__pycache__/admin.cpython-310.pyc -------------------------------------------------------------------------------- /view_set/api/__pycache__/apps.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/view_set/api/__pycache__/apps.cpython-310.pyc -------------------------------------------------------------------------------- /view_set/api/__pycache__/models.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/view_set/api/__pycache__/models.cpython-310.pyc -------------------------------------------------------------------------------- /view_set/api/__pycache__/serialize.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/view_set/api/__pycache__/serialize.cpython-310.pyc -------------------------------------------------------------------------------- /view_set/api/__pycache__/views.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/view_set/api/__pycache__/views.cpython-310.pyc -------------------------------------------------------------------------------- /view_set/api/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from api.models import Student 3 | 4 | # Register your models here. 5 | 6 | @admin.register(Student) 7 | class StudentAdmin(admin.ModelAdmin): 8 | 9 | list_display = ["id", 'name', 'roll', 'city'] -------------------------------------------------------------------------------- /view_set/api/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class ApiConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'api' 7 | -------------------------------------------------------------------------------- /view_set/api/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.3 on 2023-02-01 09:59 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Student', 16 | fields=[ 17 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('name', models.CharField(max_length=50)), 19 | ('roll', models.IntegerField()), 20 | ('city', models.CharField(max_length=50)), 21 | ], 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /view_set/api/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/view_set/api/migrations/__init__.py -------------------------------------------------------------------------------- /view_set/api/migrations/__pycache__/0001_initial.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/view_set/api/migrations/__pycache__/0001_initial.cpython-310.pyc -------------------------------------------------------------------------------- /view_set/api/migrations/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/view_set/api/migrations/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /view_set/api/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | 5 | class Student(models.Model): 6 | 7 | name = models.CharField(max_length=50) 8 | roll = models.IntegerField() 9 | city = models.CharField(max_length=50) 10 | -------------------------------------------------------------------------------- /view_set/api/serialize.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | from api.models import Student 3 | 4 | class StudentSerialize(serializers.ModelSerializer): 5 | 6 | class Meta: 7 | 8 | model = Student 9 | fields = ['id', 'name', 'roll', 'city'] -------------------------------------------------------------------------------- /view_set/api/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /view_set/api/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | from rest_framework import viewsets, status 3 | from api.models import Student 4 | from api.serialize import StudentSerialize 5 | from rest_framework.response import Response 6 | 7 | # Create your views here. 8 | 9 | class StudentViewSet(viewsets.ViewSet): 10 | 11 | def get(self, request): 12 | stu = Student.objects.all() 13 | serializer = StudentSerialize(stu, many = True) 14 | return Response(serializer.data) 15 | 16 | def retrieve(self, request, pk=None): 17 | id = pk 18 | if id is not None: 19 | stu = Student.objects.get(id=id) 20 | serializer = StudentSerialize(stu) 21 | return Response(serializer.data) 22 | 23 | def create(self, request): 24 | serializer = StudentSerialize(data= request.data) 25 | if serializer.is_valid(): 26 | serializer.save() 27 | return Response({"message": "Data Created"}, status = status.HTTP_201_CREATED) 28 | return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) 29 | 30 | def update(self, request, pk): 31 | id = pk 32 | stu = Student.objects.get(pk=id) 33 | serializer = StudentSerialize(stu, data= request.data) 34 | if serializer.is_valid(): 35 | serializer.save() 36 | return Response({"message":"Data Update"}) 37 | return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) 38 | 39 | def partial_update(self, request, pk): 40 | id = pk 41 | stu = Student.objects.get(pk=id) 42 | serializer = StudentSerialize(stu, data= request.data, partial = True) 43 | if serializer.is_valid(): 44 | serializer.save() 45 | return Response({"message": "Partial Data update"}, status= status.HTTP_200_OK) 46 | return Response(serializer.errors) 47 | 48 | def delete(self, request,pk): 49 | id = pk 50 | stu = Student.objects.get(pk=id) 51 | stu.delete() 52 | return Response({"msg":"Data Delete"}) 53 | -------------------------------------------------------------------------------- /view_set/db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/view_set/db.sqlite3 -------------------------------------------------------------------------------- /view_set/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'view_set.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /view_set/view_set/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/view_set/view_set/__init__.py -------------------------------------------------------------------------------- /view_set/view_set/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/view_set/view_set/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /view_set/view_set/__pycache__/settings.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/view_set/view_set/__pycache__/settings.cpython-310.pyc -------------------------------------------------------------------------------- /view_set/view_set/__pycache__/urls.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/view_set/view_set/__pycache__/urls.cpython-310.pyc -------------------------------------------------------------------------------- /view_set/view_set/__pycache__/wsgi.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yash-Dobariya/Django_REST_API/7af51edfb7d863a3550f19f97faf57c1e0b259c3/view_set/view_set/__pycache__/wsgi.cpython-310.pyc -------------------------------------------------------------------------------- /view_set/view_set/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for view_set project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'view_set.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /view_set/view_set/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for view_set project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.1.3. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'django-insecure-cztcc!i5py66hi45y(kn*=+rfmg@=8v2$#c^j^@p#codf7qw+1' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'rest_framework', 41 | 'api' 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'view_set.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'view_set.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/4.1/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': BASE_DIR / 'db.sqlite3', 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/4.1/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'UTC' 111 | 112 | USE_I18N = True 113 | 114 | USE_TZ = True 115 | 116 | 117 | # Static files (CSS, JavaScript, Images) 118 | # https://docs.djangoproject.com/en/4.1/howto/static-files/ 119 | 120 | STATIC_URL = 'static/' 121 | 122 | # Default primary key field type 123 | # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field 124 | 125 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 126 | -------------------------------------------------------------------------------- /view_set/view_set/urls.py: -------------------------------------------------------------------------------- 1 | """view_set URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/4.1/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path, include 18 | from api import views 19 | from rest_framework.routers import DefaultRouter 20 | 21 | router = DefaultRouter() 22 | 23 | router.register('stu',views.StudentViewSet, basename="student") 24 | 25 | 26 | urlpatterns = [ 27 | path('admin/', admin.site.urls), 28 | path('rest/',include(router.urls)) 29 | ] 30 | 31 | -------------------------------------------------------------------------------- /view_set/view_set/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for view_set project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'view_set.settings') 15 | 16 | application = get_wsgi_application() 17 | --------------------------------------------------------------------------------