Skip to content

Instantly share code, notes, and snippets.

View hemanth-sp's full-sized avatar

hemanth sp hemanth-sp

View GitHub Profile
@hemanth-sp
hemanth-sp / models.py
Last active February 24, 2023 20:26
Custom Django UserModel with roles
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
DOCTOR = 1
NURSE = 2
SURGEN =3
ROLE_CHOICES = (
(DOCTOR, 'Doctor'),
(NURSE, 'Nurse'),
@hemanth-sp
hemanth-sp / views.py
Last active October 6, 2021 15:54
Generic class-based view with views router curd
from rest_framework.views import APIView
from curd.serializers import StudentSerializers
from curd.models import Student
from rest_framework import status, permissions, generics, viewsets
from rest_framework.response import Response
from django.shortcuts import get_object_or_404
class StudentCurd(viewsets.ViewSetMixin, generics.ListCreateAPIView, generics.RetrieveUpdateDestroyAPIView):
queryset = Student.objects.all()
@hemanth-sp
hemanth-sp / django_rest_api_view_inheritance.py
Created February 27, 2021 03:06
Django rest API view inheritance or Django rest view hierarchy in detail
class View:
"""
Intentionally simple parent class for all views. Only implements
dispatch-by-method and simple sanity checking.
"""
http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
class APIView(View):
@hemanth-sp
hemanth-sp / views.py
Created February 3, 2021 17:28
ModelViewset curd
from rest_framework.views import APIView
from curd.serializers import StudentSerializers
from curd.models import Student
from rest_framework import status, permissions, generics, viewsets
from rest_framework.response import Response
from django.shortcuts import get_object_or_404
class StudentCurd(viewsets.ModelViewSet):
queryset = Student.objects.all()
@hemanth-sp
hemanth-sp / views.py
Created February 3, 2021 17:24
Viewset curd
from rest_framework.views import APIView
from curd.serializers import StudentSerializers
from curd.models import Student
from rest_framework import status, permissions, generics, viewsets
from rest_framework.response import Response
from django.shortcuts import get_object_or_404
class StudentCurd(viewsets.ViewSet):
permission_classes = [permissions.AllowAny]
@hemanth-sp
hemanth-sp / views.py
Created February 3, 2021 17:07
Generic class-based view curd
from rest_framework.views import APIView
from curd.serializers import StudentSerializers
from curd.models import Student
from rest_framework import status, permissions, generics
from rest_framework.response import Response
from django.shortcuts import get_object_or_404
class StudentCurd(generics.ListCreateAPIView):
queryset = Student.objects.all()
@hemanth-sp
hemanth-sp / view.py
Created February 3, 2021 17:01
class-based API view curd
from rest_framework.views import APIView
from curd.serializers import StudentSerializers
from curd.models import Student
from rest_framework import status, permissions
from rest_framework.response import Response
from django.shortcuts import get_object_or_404
class StudentCurd(APIView):
permission_classes = [permissions.AllowAny]
@hemanth-sp
hemanth-sp / views.py
Created February 3, 2021 16:55
Function-based curd
from curd.serializers import StudentSerializers
from curd.models import Student
from rest_framework import status
from rest_framework.response import Response
from rest_framework.decorators import api_view
from django.shortcuts import get_object_or_404
@api_view(['GET', 'POST'])
def students_list_or_create(request):
@hemanth-sp
hemanth-sp / .py
Created February 26, 2020 17:42
views.py
from django.contrib.auth.models import User
from rest_framework import generics
from rest_framework.permissions import IsAdminUser, AllowAny
from .serializers import *
from .models import *
from rest_framework.renderers import JSONRenderer
from rest_framework.utils import encoders, json
"""
@hemanth-sp
hemanth-sp / comment.html
Last active January 4, 2020 03:05
like and dislike comment with tool tip html
<div class="container-1 left">
<div class="f-card content">
<h2>{{page_obj.created_at}}</h2>
<p>{{page_obj.comment}}.</p>
<div class="text-left">
<span class="pointer">
{% if request.user in page_obj.likes.users.all%}
<!--Start of already liked-->
<a href={% url 'comment_vote' comment_id=page_obj.pk opition='like' %}>