Search code examples
djangodjango-registration

django 1.10 - custom sql inserts when user registered


I am using django 1.10 + django-registration 2.2 (HMAC activation workflow). How can I make custom sql inserts when a new user is registered? For example every user has own settings and after registration I want to insert the default settings in the sql table for this user. Later the user can change these settings.

What is the common approach? Do I have to edit the django-registration 2.2 files?


Solution

  • Use django signals.

    from django.contrib.auth.models import User
    from django.db.models.signals import post_save
    
    
    @post_save(sender=User)
    def my_callback(sender, instance, created, **kwargs):
        """
    
        :param sender: 
        :param instance: user instance
        :param created: Inserted or Updated
        :param kwargs: 
        :return: 
        """
        if created is True:
            # Create custom settings for user.
            pass
    

    https://docs.djangoproject.com/en/1.11/ref/signals/#django.db.models.signals.pre_save