python
34 lines · 1 tab
Priya Sharma
Jan 2026
1 tab
from django.db import models
from django.utils import timezone
from datetime import date
class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
date_of_birth = models.DateField()
email = models.EmailField()
@property
def full_name(self):
"""Computed full name from first and last."""
return f'{self.first_name} {self.last_name}'
@property
def age(self):
"""Calculate current age from date of birth."""
today = date.today()
age = today.year - self.date_of_birth.year
if today.month < self.date_of_birth.month or \
(today.month == self.date_of_birth.month and
today.day < self.date_of_birth.day):
age -= 1
return age
@property
def email_domain(self):
"""Extract domain from email address."""
return self.email.split('@')[1] if '@' in self.email else ''
def __str__(self):
return self.full_name
1 file · python
Explain with highlit
Properties let me add computed attributes to models without storing them in the database. I use @property for simple calculations like full name or age. For expensive computations, I consider caching the result in a field and updating it via signals or save override. Properties can't be used in QuerySet filters—use annotate() for that. I keep property logic simple and stateless. For display-only values, properties are cleaner than adding methods. They work seamlessly in templates and serializers, appearing just like regular fields.
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.