python
40 lines · 2 tabs
Priya Sharma
Jan 2026
2 tabs
from django.db import models
from django.utils import timezone
class TimeStampedModel(models.Model):
"""Abstract base class with created/updated timestamps."""
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
class SoftDeleteModel(models.Model):
"""Abstract base class for soft delete functionality."""
deleted_at = models.DateTimeField(null=True, blank=True)
class Meta:
abstract = True
def delete(self, using=None, keep_parents=False):
"""Soft delete by setting deleted_at timestamp."""
self.deleted_at = timezone.now()
self.save()
def hard_delete(self):
"""Permanently delete from database."""
super().delete()
from django.db import models
from core.models import TimeStampedModel, SoftDeleteModel
class Post(TimeStampedModel, SoftDeleteModel):
"""Post model with timestamps and soft delete."""
title = models.CharField(max_length=200)
content = models.TextField()
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
def __str__(self):
return self.title
2 files · python
Explain with highlit
Abstract base classes let me define common fields and methods without creating database tables. I set abstract = True in Meta. Concrete models inheriting from the abstract class get all its fields and methods. This is perfect for timestamps, soft deletes, or common metadata fields. Unlike multi-table inheritance, this doesn't create joins or extra queries. For polymorphic behavior, I use Django Polymorphic library. Abstract models can't be instantiated directly and don't appear in migrations as separate tables. This keeps the schema clean and code DRY.
Related snips
python
import os
import stat
for root, _dirs, files in os.walk('/etc'):
for name in files:
path = os.path.join(root, name)
Python security audit script for exposed risky filesystem state
python
auditing
host-security
by Kai Nakamura
1 tab
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.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
Django URL namespacing and reverse lookups
django
python
urls
by Priya Sharma
3 tabs
python
import graphene
from graphene_django import DjangoObjectType
from blog.models import Post, Comment
class PostType(DjangoObjectType):
Django GraphQL with Graphene
django
python
graphql
by Priya Sharma
2 tabs
python
from django.db.models import Count, Avg, Sum, Q, F
from django.views.generic import TemplateView
from products.models import Product, Order, OrderItem
class DashboardView(TemplateView):
Django aggregation with annotate for statistics
django
python
database
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
Share this code
Here's the card — post it anywhere.