Skip to content

Instantly share code, notes, and snippets.

View toransahu's full-sized avatar
💭
I may be slow to respond.

Toran Sahu toransahu

💭
I may be slow to respond.
View GitHub Profile
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@toransahu
toransahu / restful_view_func_api_view_decorator_way.py
Last active January 26, 2018 17:01
Django REST framework VIEW Implementation Approach: Function Based + @api_view() decorator
from rest_framework import status [ from rest_framework import status]
from rest_framework.decorators import api_view [ from rest_framework.decorators import api_view]
from rest_framework.response import Response [ from rest_framework.response import Response]
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
@api_view(['GET', 'POST'])
def snippet_list(request):
"""
@toransahu
toransahu / restful_view_func_normal_way.py
Created January 26, 2018 17:00
Django REST framework VIEW Implementation Approach: Function Based + Normal Django View
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
@csrf_exempt
def snippet_list(request):
"""
@toransahu
toransahu / restful_view_class_APIView_way.py
Last active January 26, 2018 17:24
Django REST framework VIEW Implementation Approach: Class Based + Inherit APIView
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status [ from rest_framework import status]
class SnippetList(APIView):
"""
@toransahu
toransahu / restful_view_class_mixins_way.py
Created January 26, 2018 17:32
Django REST framework VIEW Implementation Approach: Class Based + Inherit generics.GenericAPIView & mixin classes
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
from rest_framework import mixins [ from rest_framework import mixins]
from rest_framework import generics [ from rest_framework import generics]
class SnippetList(mixins.ListModelMixin,
mixins.CreateModelMixin,
generics.GenericAPIView):
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer
@toransahu
toransahu / restful_generic_class_based_view.py
Created January 26, 2018 18:13
Django REST framework VIEW Implementation Approach: Class Based + Total Generic (Inherit generics.ListCreateAPIView, generics.RetrieveUpdateDestroyAPIView)
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
from rest_framework import generics
class SnippetList(generics.ListCreateAPIView):
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer
@toransahu
toransahu / django_custom_query_manager.py
Created January 27, 2018 17:52
Django - Custom Query Manager Example
from django.db import models
class PollManager(models.Manager):
def with_counts(self):
from django.db import connection
with connection.cursor() as cursor:
cursor.execute("""
SELECT p.id, p.question, p.poll_date, COUNT(*)
FROM polls_opinionpoll p, polls_response r
WHERE p.id = r.poll_id
@toransahu
toransahu / extend_user_model_using_proxy_model.py
Created January 27, 2018 17:56
Django | Extend User Model | Using a Proxy Model
from django.contrib.auth.models import User
from .managers import PersonManager
class Person(User):
objects = PersonManager()
class Meta:
proxy = True
ordering = ('first_name', )
@toransahu
toransahu / extend_user_model_using_OneToOneField.py
Last active January 22, 2023 07:02
Django | Extend User Model | Using a One-To-One Link | Profile
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birth_date = models.DateField(null=True, blank=True)
@toransahu
toransahu / extend_user_model_using_Custom_Model_Extending_AbstractBaseUser.py
Last active January 27, 2018 20:00
Django | Extend User Model | Extending User Model Using a Custom Model Extending AbstractBaseUser
from __future__ import unicode_literals
from django.db import models
from django.core.mail import send_mail
from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth.base_user import AbstractBaseUser
from django.utils.translation import ugettext_lazy as _
from .managers import UserManager