python
38 lines · 2 tabs
Priya Sharma
Jan 2026
2 tabs
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)
def save(self, *args, **kwargs):
# Auto-generate slug (model-specific logic)
if not self.slug:
from django.utils.text import slugify
self.slug = slugify(self.name)
# Calculate margin (business logic)
if self.price and self.cost:
self.margin = ((self.price - self.cost) / self.price) * 100
super().save(*args, **kwargs)
from django.db.models.signals import post_save
from django.dispatch import receiver
from products.models import Product
@receiver(post_save, sender=Product)
def update_inventory_cache(sender, instance, created, **kwargs):
"""Update cache when product changes (cross-app concern)."""
from django.core.cache import cache
cache.delete(f'product:{instance.id}')
@receiver(post_save, sender=Product)
def notify_price_change(sender, instance, created, **kwargs):
"""Send notification on price change (separate concern)."""
if not created:
# Check if price changed
old_product = Product.objects.get(pk=instance.pk)
if old_product.price != instance.price:
send_price_alert.delay(instance.id)
2 files · python
Explain with highlit
Signals and save overrides both handle model events, but have different use cases. I override save() for logic intrinsic to the model. Signals decouple logic across apps—I use them when multiple apps need to respond to model changes. Signals can make debugging harder due to implicit connections. I use post_save with created flag to distinguish create vs update. For performance-critical paths, save override is faster (no signal dispatch). I document signals clearly. Choose based on coupling needs—tight coupling favors save override, loose coupling favors signals.
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
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
python
from django.db import models
from django.utils.text import slugify
class Article(models.Model):
title = models.CharField(max_length=200)
Django model save override for custom logic
django
python
models
by Priya Sharma
1 tab
Share this code
Here's the card — post it anywhere.