I have user app.
In signals.py I have
from django.db.models.signals import pre_save
from user.models import User
from django.dispatch import receiver
import random
import string
@receiver(pre_save,sender=User)
def create_hash_for_user(sender,instance,**kwargs):
allowed_chars = ''.join((string.ascii_letters, string.digits))
unique_id = ''.join(random.choice(allowed_chars) for _ in range(32))
print("Request finished!")
instance.user_hash = unique_id
instance.save()
In apps.py
from django.apps import AppConfig
class UserConfig(AppConfig):
name = 'user'
def ready(self):
import user.signals
In models.py I have extended the abstractbaseuser
from django.db import models
from django.shortcuts import render
from django.core.exceptions import ValidationError
from django.contrib.auth.models import (
AbstractBaseUser,
BaseUserManager
)
from .utils import file_size
class MyUserManager(BaseUserManager):
def create_user(self, username,email,password=None):
"""
Creates and saves a User with the given email, date of
birth and password.
"""
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=self.normalize_email(email),
username=username
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self,username,email,password):
"""
Creates and saves a superuser with the given email, date of
birth and password.
"""
user = self.create_user(
username=username,
email=email,
password=password,
)
user.is_admin = True
user.save(using=self._db)
return user
class User(AbstractBaseUser):
username=models.CharField(max_length=200,blank=True,null=True)
first_name = models.CharField(max_length=100,blank=True,null=True)
last_name = models.CharField(max_length=100, blank=True, null=True)
email = models.EmailField(unique=True)
image = models.ImageField(upload_to='images',blank=True,validators=[file_size])
date_joined = models.DateTimeField(auto_now=False,auto_now_add=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
user_hash = models.CharField(max_length=512,blank=True,null=True)
USERNAME_FIELD='email'
objects = MyUserManager()
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
@property
def is_staff(self):
return self.is_admin
But signal function is not called, and Request finished! is not printed and user_hash is not created.
Furthurmore, I recommend change to post_save
, not pre_save
In your code, each user make there own user_hash for every save() method for user. I guess you didn't intend this.
user_hash
have to created only right after creation. So you can use post_save
with created
command. Like this
@receiver(post_save, sender=User)
def create_hash_post_save(sender, instance, created, **kwargs):
if created:
allowed_chars = ''.join((string.ascii_letters, string.digits))
unique_id = ''.join(random.choice(allowed_chars) for _ in range(32))
print("Request finished!")
instance.user_hash = unique_id
instance.save()
Or, you should check your instance have pk
before add user_hash. Like this
@receiver(pre_save,sender=User)
def create_hash_for_user(sender,instance,**kwargs):
if not instance.pk:
allowed_chars = ''.join((string.ascii_letters, string.digits))
unique_id = ''.join(random.choice(allowed_chars) for _ in range(32))
print("Request finished!")
instance.user_hash = unique_id
In your __init__.py
of user app, you must set the app config you created in apps.py
# __init__.py
default_app_config = 'user.apps.UserConfig'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.