1👍
Django provides of built-in signals : pre_save,pre_init…
use this like:
def password_change_signal(sender, instance, **kwargs):
try:
user = User.objects.get(username=instance.username)
if not hasattr(user, 'userprofile'):
print 'has no profile'
return
if not user.password == instance.password:
print 'setting vals'
profile = user.get_profile()
profile.force_password_change = False
profile.save()
except User.DoesNotExist:
pass
signals.pre_save.connect(password_change_signal, sender=User,dispatch_uid='members.models.UserProfile')
0👍
According to this SO POST, You can use post_save.connect()
to save customer object whenever a user is created. Please note that I have pasted and edited code in aforementioned post according to your requirements.
from django.db.models.signals import post_save
from .models import Customer
from django.contrib.auth.models import User
def create_customer(sender, **kwargs):
user = kwargs["instance"]
if kwargs["created"]:
customer = user.models.Customer()
customer.setUser(sender)
customer.save()
post_save.connect(create_customer, sender=User)
- Django 2.7 Rest API Validation Request
- Access a value from a list field in django
- Csrf token missing in django ajax post request working using get request
Source:stackexchange.com