Ryan Saxe
Ryan Saxe

Reputation: 17869

Django check when user created

So in any Django project, it is fairly simple to tell if an object has been created by overriding the save method:

def save(self, *args, **kwargs):
    created = False
    if not self.pk:
        created = True
    super(ModelName, self).save(*args,**kwargs)
    if created:
        #do what you want or call a signal

But I need to call a function after a User is created from django.contrib.auth.models.User. I would prefer not to have to actually edit the user model's save method.

How could I go about doing this?

Note:

using pre_save will not work because none of the info from the model will be available, and that is necessary.

Upvotes: 1

Views: 917

Answers (1)

Nigel Tufnel
Nigel Tufnel

Reputation: 11534

You can use post_save signal.

from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save)
def do_your_thing(sender, **kwargs):
    if sender is User:
        if kwargs["created"]:
            print kwargs["instance"].password
            # do your thing

Upvotes: 5

Related Questions