Tutorial by Examples

This example is a snippet taken from the Extending Django User Profile like a Pro from django.db import models from django.contrib.auth.models import User from django.db.models.signals import post_save class UserProfile(models.Model): user = models.OneToOneField(User, related_name='user'...
from django.db import models from django.contrib.auth.models import User from django.db.models.signals import post_save from django.dispatch import receiver class UserProfile(models.Model): user = models.OneToOneField(User, related_name='user') website = models.URLField(default='', ...
By utilizing the pre_save we can determine if a save action on our database was about updating an existing object or creating a new one. In order to achieve this you can check the state of the model object: @receiver(pre_save, sender=User) def pre_save_user(sender, instance, **kwargs): ...
Django's signals are restricted to precise class signatures upon registration, and thus subclassed models are not immediately registered onto the same signal. Take this model and signal for example class Event(models.Model): user = models.ForeignKey(User) class StatusChange(Event): ...

Page 1 of 1