1👍
Yes, you need to use django signals, specifically post_save(). This, as you probably guessed, get’s called after the save of your model and you can then implement whatever post save functionality (that is, post write to the database) you require.
1👍
ok 5 years latter but this works for me with django 1.8 and python 2.7
The context is: the user create a new account then the admin receives an email to verify the user and chage active to True when the admin makes the change the user receives an email telling that now he can log in.
Sorry for my bad english.
#forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
#A register form that save field is_active as False
class RegisterForm(UserCreationForm):
email = forms.EmailField(label=_("E-mail"))
first_name = forms.CharField(label=_("First Name"))
last_name = forms.CharField(label=_("Last Name"))
is_active = forms.BooleanField(required=False, initial=False, label=_("Activo"), widget=forms.HiddenInput())
class Meta:
model = User
fields = ('username','first_name','last_name','email','password1','password2','is_active')
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.email = self.cleaned_data['email']
user.is_active = self.cleaned_data['is_active']
if commit:
user.save()
return user
I use the signals in models.py file but you can use it in a signals.py file
#models.py
from django.contrib.auth.models import User
from django.db.models import signals
from django.db import models
from django.dispatch import receiver
from django.db.models.signals import pre_save, post_save
from django.conf import settings
from django.core.mail import send_mail
#signal used for is_active=False to is_active=True
@receiver(pre_save, sender=User, dispatch_uid='active')
def active(sender, instance, **kwargs):
if instance.is_active and User.objects.filter(pk=instance.pk, is_active=False).exists():
subject = 'Active account'
mesagge = '%s your account is now active' %(instance.username)
from_email = settings.EMAIL_HOST_USER
send_mail(subject, mesagge, from_email, [instance.email], fail_silently=False)
#signal to send an email to the admin when a user creates a new account
@receiver(post_save, sender=User, dispatch_uid='register')
def register(sender, instance, **kwargs):
if kwargs.get('created', False):
subject = 'Verificatión of the %s account' %(instance.username)
mesagge = 'here goes your message to the admin'
from_email = settings.EMAIL_HOST_USER
send_mail(subject, mesagge, from_email, [from_email], fail_silently=False)
- [Answered ]-Django: how do I code methods in abstract class which rely on a manager?
- [Answered ]-Django.contrib.auth.views.login returns Anonymous user
0👍
This has not been tested but here is a modified version of something I do that is similar:
from django.contrib.auth.models import User
from django.db.models import signals
from django.conf import settings
from django.core.mail import send_mail
def pre_user_save(sender, instance, *args, **kwargs):
if instance.active != User.objects.get(id=instance.id).active:
send_mail(
subject='Active changed: %s -> %s' % (instance.username, instance.active),
message='Guess who changed active status??',
from_email=settings.SERVER_EMAIL,
recipient_list=[p[1] for p in settings.MANAGERS],
)
signals.pre_save.connect(pre_user_save, sender=User, dispatch_uid='pre_user_save')
Hope this helps!
- [Answered ]-Django – Unable to override settings.py values from within it
- [Answered ]-Django, South: I get a warning with key length while indexing
- [Answered ]-Django JWT asking for username/password
Source:stackexchange.com