Priya Sharma

83 code snips · on codesnips 6 months

Python specialist focused on Django, REST APIs, async patterns, and scalable backend architecture.

python
import os
from .base import *

DEBUG = False

SECRET_KEY = os.environ['SECRET_KEY']

Django deployment checklist and production settings

django python deployment
by Priya Sharma 1 tab
python
from haystack import indexes
from blog.models import Post


class PostIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)

Django haystack for advanced search

django python search
by Priya Sharma 2 tabs
python
INSTALLED_APPS += ['drf_spectacular']

REST_FRAMEWORK = {
    'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
}

Django REST Framework schema and documentation

django python rest
by Priya Sharma 3 tabs
python
import random


class ReadWriteRouter:
    """Route reads to replicas, writes to primary."""

Django database read-write split with routers

django python database
by Priya Sharma 3 tabs
python
from django.contrib.auth.models import AbstractUser
from django.db import models


class CustomUser(AbstractUser):
    """Extended user model with additional fields."""

Django custom user model best practices

django python models
by Priya Sharma 2 tabs
python
from django.core.mail import EmailMessage, EmailMultiAlternatives
from django.template.loader import render_to_string
from django.conf import settings
import os

Django email with attachments and templates

django python email
by Priya Sharma 1 tab
python
from rest_framework import permissions


class IsOwner(permissions.BasePermission):
    """Allow only object owner to access."""

Django REST Framework permissions and authorization

django python rest
by Priya Sharma 2 tabs
python
class Product(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField(blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    cost = models.DecimalField(max_digits=10, decimal_places=2)
    margin = models.DecimalField(max_digits=5, decimal_places=2, blank=True)

Django model signals vs overriding save

django python models
by Priya Sharma 2 tabs
python
from django.db import models
from django.contrib.postgres.indexes import GinIndex, BTreeIndex


class Order(models.Model):
    customer = models.ForeignKey('Customer', on_delete=models.CASCADE)

Django database indexes for query performance

django python database
by Priya Sharma 1 tab
python
from django.db import transaction
from django.http import JsonResponse


@transaction.atomic
def transfer_funds(request):

Django atomic transactions for data integrity

django python database
by Priya Sharma 1 tab
python
# Optional: custom error handlers
handler404 = 'myapp.views.custom_404'
handler500 = 'myapp.views.custom_500'


# In views.py

Django custom error pages (404, 500)

django python templates
by Priya Sharma 3 tabs
python
from blog.models import Post


# Load only specific fields
posts = Post.objects.only('id', 'title', 'published_at')
for post in posts:

Django query optimization with only and defer

django python database
by Priya Sharma 1 tab