models

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.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
python
from django.db import models
from django.utils import timezone


class PostQuerySet(models.QuerySet):
    def published(self):

Django model managers for custom querysets

django python models
by Priya Sharma 1 tab
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.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.db import models


class CustomUserManager(BaseUserManager):
    def create_user(self, email, password=None, **extra_fields):

Django custom user model with email authentication

django python authentication
by Priya Sharma 1 tab
python
from django.db import models
from django.core.exceptions import ValidationError
from django.utils import timezone


class Event(models.Model):

Django model validation with clean method

django python models
by Priya Sharma 1 tab
python
from django.db import models


class Post(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()

Django model inheritance with proxy models

django python models
by Priya Sharma 1 tab
python
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType


class Comment(models.Model):

Django contenttypes framework for generic relations

django python models
by Priya Sharma 3 tabs
python
from django.db import models
from django.utils.text import slugify


class Article(models.Model):
    title = models.CharField(max_length=200)

Auto-Generate Unique Slugs in Django with a pre_save Signal and Model Method

django signals slugs
by codesnips 3 tabs
python
from django.db import models
from django.utils import timezone


class SoftDeleteManager(models.Manager):
    def get_queryset(self):

Django model soft delete pattern

django python models
by Priya Sharma 1 tab
python
from django.db import models


class Product(models.Model):
    name = models.CharField(max_length=200)
    sku = models.CharField(max_length=50)

Django model meta options for database optimization

django python models
by Priya Sharma 1 tab
python
from django.db import models
from django.utils import timezone
from datetime import date


class Person(models.Model):

Django model property for computed fields

django python models
by Priya Sharma 1 tab