Reputation: 2417
I have been trying to send all the user data to his/her provided email whenever admin creates a user detail through django admin.I reckon post_save signal should be used for such process.But my post_save signal is not called on console and also could not send all the user details after admin has created .What have i done wrong ?
Models.py
from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.core.mail import EmailMessage
from django.dispatch import receiver
from django.db import models
@receiver(post_save,sender=User)
def send_user_data_when_created_by_admin(sender, **kwargs):
import inspect
records = []
for record in inspect.stack():
print('record',record)
records.append(record[3])
if record[3]=='get_response':
request = record[0].f_locals['request']
first_name = request.POST.get('first_name')
print('first name is',first_name)
last_name = request.POST.get('last_name')
address = request.POST.get('address')
email = request.POST.get('email')
html_content = "your first name:%s <br> last name:%s <br> address:%s"
from_email = settings.DEFAULT_FROM_EMAIL
message=EmailMessage('welcome',html_content %(first_name,last_name,address),from_email,[email])
message.content_subtype='html'
message.send()
class userbase(models.Model):
first_name = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the first name of user"))
last_name = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the last name"))
address = models.CharField(max_length=300,blank=True,null=True,help_text=("enter the address"))
contact = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the contact"))
email = models.EmailField(max_length=100,blank=True,null=True,help_text=("enter the email"))
username = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the username"))
password = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the strong password"))
def __str__(self):
return (self.username)
Upvotes: 3
Views: 4420
Reputation: 621
you should change your function to this
@receiver(post_save,sender=UserProfile)
def send_user_data_when_created_by_admin(sender, instance, **kwargs):
first_name = instance.user.first_name
print('first name is',first_name)
last_name = instance.user.last_name
address = instance.address
email = instance.user.email
html_content = "your first name:%s <br> last name:%s <br> address:%s"
message=EmailMessage(subject='welcome',body=html_content %(first_name,last_name,address),to=[email])
message.content_subtype='html'
message.send()
your model should be this
class UserProfile(models.Model):
address = models.CharField(max_length=300,blank=True,null=True,help_text=("enter the address"))
contact = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the contact"))
user = models.ForeignKey(User)
def __str__(self):
return (self.username)
so whenever you want to save a new object, you use this
user = User.objects.create_user(username=username, email=email, password=password, first_name=first_name, last_name=last_name)
UserProfile.objects.create(user=user, contact=contact, address=address)
user.save()
where username, email, password, first_name, last_name, contact and address are the cleaned_data of their respective field value from the form you created.
You could design your admin to be forced to create a UserProfile while creating a User by using these lines in your admin.py
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from account.models import UserProfile
class UserProfileInline(admin.StackedInline):
model = UserProfile
can_delete = False
class UserProfileAdmin(UserAdmin):
inlines = (UserProfileInline,)
admin.site.unregister(get_user_model())
admin.site.register(get_user_model(), UserProfileAdmin)
Should in case you run into any error kindly let me know
Upvotes: 2